【发布时间】:2016-10-22 17:13:18
【问题描述】:
下面是代码
#include <iostream>
#include <pthread.h>
using namespace std;
class Base
{
private:
public:
void *threadCall1( void * value)
{
cout<<"inside threadCall1"<<endl;
}
protected:
};
class Derived
{
private:
public:
void *threadCall2 ();
protected:
};
void *Derived::threadCall2()
{
cout<<"inside threadCall2"<<endl;
}
int main ()
{
int k = 2;
pthread_t t1;
cout<<"inside main"<<endl;
Base *b = new Base();
pthread_create(&t1,NULL,&b->threadCall1,(void *)k);
return 0;
}
错误
main.cc:在函数
int main()': main.cc:46: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say&Base::threadCall1' main.cc:46:错误:不能 将void*(Base::*)(void*)' tovoid*()(void)' 转换为参数3' toint pthread_create(pthread_t*, const pthread_attr_t*, void*()(void), void*)'
我同意 C++ 禁止此调用,但有什么方法可以使用 posix 线程调用类成员函数
【问题讨论】:
-
不,你不能那样做。 Posix 线程对 C++ 一无所知。为什么不使用 std::thread?
-
我的意思是,不能直接di。当然,您可以从符合 Posix 的线程回调中调用您的成员函数。
标签: c++ multithreading