我正在创建一个多生产者多消费者Pub Sub框架。为了测试多个生产者发布消息,我试图创建5个线程和5个发布者对象。然后,我在每个线程中传递每个对象的发布功能。
但是在调用pthread_create时遇到错误
main.cpp: In function ‘int main()’:
main.cpp:30:68: error: invalid use of non-static member function ‘void* publisher::publish(void*)’
int tid=pthread_create(&thread[i],NULL,(pobj[i]->publish),pstr[i]);
我尝试提供地址
int tid=pthread_create(&thread[i],NULL,&(pobj[i]->publish),pstr[i]);
但是它给出了错误提示
main.cpp: In function ‘int main()’:
main.cpp:30:60: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&publisher::publish’ [-fpermissive]
int tid=pthread_create(&thread[i],NULL,&(pobj[i]->publish),pstr[i]);
^
main.cpp:30:69: error: cannot convert ‘void* (publisher::*)(void*)’ to ‘void* (*)(void*)’ for argument ‘3’ to ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
int tid=pthread_create(&thread[i],NULL,&(pobj[i]->publish),pstr[i]);
我的主:
#include<iostream>
#include"message.h"
#include"pubsubservice.h"
#include"subscriber.h"
#include"publisher.h"
#include<pthread.h>
#include"pubstruc.h"
using namespace std;
//class publisher;
typedef void * (*THREADFUNCPTR)(void *);
# define TOTAL_THREADS 5
int main()
{
pthread_t thread[TOTAL_THREADS];
//publisher pub[5];
pubsubservice service;
publisher * pobj[5];
//message cplusplusMsg[5];
message cplusplusMsg[5]={{"cplusplus", "Core cplusplus Concepts"},
{"cplusplus", "Dependency and AOP"},{"cplusplus", "STL library"},
{"cplusplus","Boost"},{"cplusplus","pubsub"}};
publish_str *pstr[5];
for(int i=0;i<TOTAL_THREADS;i++)
{
pstr[i]->message_obj=cplusplusMsg[i];
pstr[i]->sevice_obj=service;
int tid=pthread_create(&thread[i],NULL,&(pobj[i]->publish),pstr[i]);
}
// publisher cpluspluspublisher;
// publisher pythonpublisher;
// subscriber cplusplussubscriber("cplusplussubscriber");
// subscriber allLanguagessubscriber("allLanguagessubscriber");
// subscriber pythonsubscriber("pythonsubscriber");
// pubsubservice service;
// //Declare Messages and Publish Messages to pubsubservice
// message cplusplusMsg1("cplusplus", "Core cplusplus Concepts");
// message cplusplusMsg2("cplusplus", "Dependency and AOP");
// message cplusplusMsg3("cplusplus", "STL library");
// cpluspluspublisher.publish(cplusplusMsg1, service);
// cout << service.messagesQueue.size()<<endl;
// cpluspluspublisher.publish(cplusplusMsg2, service);
// cpluspluspublisher.publish(cplusplusMsg3, service);
// cout << service.messagesQueue.size() << endl;
return 0;
}
publisher.h
#pragma once
#ifndef _PUBLISHER_H
#define _PUBLISHER_H
#include<iostream>
#include"pubsubservice.h"
#include"message.h"
//#include"pubstruc.h"
struct publish_str;
using namespace std;
class publisher {
public:
void * publish(void * obj);
};
#endif
publisher.cpp:
#include<iostream>
#include"message.h"
#include"pubsubservice.h"
#include"publisher.h"
#include"pubstruc.h"
using namespace std;
struct publish_str;
void *publisher::publish( void * obj1)
{
publish_str * obj= (publish_str *) obj1;
obj->sevice_obj.adMessageToQueue(obj->message_obj);
//pubSubService.adMessageToQueue(Message);
}
pubstruc.h
#pragma once
#ifndef _PUBSTRUC_H
#define _PUBSTRUC_H
#include<iostream>
#include"pubsubservice.h"
#include"message.h"
using namespace std;
struct publish_str{
message message_obj;
pubsubservice sevice_obj;
};
#endif;
message.h:
#pragma once
#ifndef _MESSAGE_H
#define _MESSAGE_H
#include<iostream>
using namespace std;
class message {
public:
//message();
message(string topic, string payload);
string getTopic();
void setTopic(string topic);
string getPayload();
void setPayload(string Payload);
bool operator ==(message right) const
{
return ((payload == right.payload)&&(topic==right.topic));
}
private:
string topic;
string payload;
};
#endif