【发布时间】:2012-08-28 13:20:40
【问题描述】:
我有以下课程:
class A
{
private:
int starter()
{
//TO_DO: pthread_create()
}
void* threadStartRoutine( void *pThis );
}
我想从 starter() 内部创建一个线程来运行 threadStartRoutine()。我得到关于第三个参数的编译时错误,它应该采用启动例程的地址。
调用 pthread_create() 来创建一个开始执行 threadStartRoutine() 的新线程的正确方法是什么?
我在网上看到一些文章说大多数编译器不允许使用 pthread_create() 调用非静态成员函数。这是真的?这背后的原因是什么?
我正在使用 G++ 在 Linux-x64 上编译我的程序。
【问题讨论】:
-
使线程启动例程静态
-
@neagoegab:我无法将 threadStartRoutine 设为静态。该类是多线程的。将 threadStartRoutine 设为静态会导致我试图消除的内存泄漏错误。
-
除非你因为某种原因被困在过去,否则你应该考虑标准线程库。即使你被困在过去,Boost.Thread 也可能是比围绕 pthread 滚动你自己的包装器更好的选择。
标签: c++ multithreading pthreads