3

在 boost::thread 中,是否可以调用类方法而无需使类可调用并实现 void operator()() 就像调用类方法一样

   for(int i=0;i<5;i++)
    boost::thread worker(myclass.myfunc,i,param2);

我收到一个错误<unresolved overloaded function type>

其实我更愿意知道同样的 zi::thread

4

2 回答 2

3

boost::thread不需要任何特殊的东西,它会完全按照你的意愿工作(减去语法错误):

for (int i = 0; i != 5; ++i)
    boost::thread worker(&myclass::myfunc, myclassPointer, i, param2);

boost.thread 文档

template <class F,class A1,class A2,...>
thread(F f,A1 a1,A2 a2,...);

效果:好像thread(boost::bind(f, a1, a2, ...))。因此,f每个aN都被复制到内部存储中以供新线程访问。

于 2011-04-15T05:28:37.340 回答
3

对于 boost::thread,您可以使用 boost::bind 调用类成员函数。

myclass obj;
for(int i=0;i<5;i++)
        boost::thread worker(boost::bind(&myclass::myfunc,&obj,i,param2));
于 2011-04-15T04:47:18.407 回答