rav*_*int 23
这可以通过使用boost库来完成,如下所示:
#include <boost/thread.hpp>
// define class to model or control a particular kind of widget
class cWidget
{
public:
void Run();
}
// construct an instance of the widget modeller or controller
cWidget theWidget;
// start new thread by invoking method run on theWidget instance
boost::thread* pThread = new boost::thread(
&cWidget::Run, // pointer to member function to execute in thread
&theWidget); // pointer to instance of class
Run Code Online (Sandbox Code Playgroud)
笔记:
在C++ 11中,你可以做同样但没有提升
// define class to model or control a particular kind of widget
class cWidget
{
public:
void Run();
}
// construct an instance of the widget modeller or controller
cWidget theWidget;
// start new thread by invoking method run on theWidget instance
std::thread * pThread = new std::thread(
&cWidget::Run, // pointer to member function to execute in thread
&theWidget); // pointer to instance of class
Run Code Online (Sandbox Code Playgroud)
shm*_*elp 15
我通常使用类的静态成员函数,并使用指向该类的指针作为void*参数.然后,该函数可以执行线程处理,或者使用类引用调用另一个非静态成员函数.然后,该函数可以引用所有类成员而不会出现笨拙的语法.
Ada*_*eld 11
你必须使用void*参数来引导它:
class A
{
static void* StaticThreadProc(void *arg)
{
return reinterpret_cast<A*>(arg)->ThreadProc();
}
void* ThreadProc(void)
{
// do stuff
}
};
...
pthread_t theThread;
pthread_create(&theThread, NULL, &A::StaticThreadProc, this);