我有以下类(class):
class A
{
private:
int starter()
{
//TO_DO: pthread_create()
}
void* threadStartRoutine( void *pThis );
}
我想从 starter() 内部创建一个线程来运行 threadStartRoutine()。我收到关于第三个参数的编译时错误,它应该采用启动例程的地址。
调用 pthread_create() 以创建开始执行 threadStartRoutine() 的新线程的正确方法是什么?
我在网上看到一些文章说大多数编译器不允许使用 pthread_create() 调用非静态成员函数。这是真的?这背后的原因是什么?
我正在使用 G++ 在 Linux-x64 上编译我的程序。
最佳答案
将 threadStartRountine()
声明为 static
:
static void* threadStartRoutine( void *pThis );
否则,threadStartRoutine()
的类型是:
void* (A::*)(void*)
这不是 pthread_create()
需要的函数指针类型。
关于C++:使用 pthread_create 创建新线程,以运行类成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160175/