【发布时间】:2011-07-09 16:04:43
【问题描述】:
就像您可能知道的那样,boost 线程要求将 fwd 作为参数的 memeber 函数必须是静态的。如果它不是静态的,则有一种绑定方式,但我更喜欢 Object o; o.startThread() 比 对象o; boost::thread(boost::bind....) 因为它将线程代码保留在类中(也是异常处理)。 因此,例如,这可以重写以工作吗:
class sayHello
{
string name;
public:
sayHello(string name_):name(name_)
{
}
void repeatHello()
{
while (true)
{
boost::this_thread::sleep(posix_time::seconds(3));
cout<<"Hello "<<name<<endl;
}
}
void infiniteRun()
{
boost::thread thr(repeatHello);//broken line
}
};
附:对于徘徊什么是“绑定方式”AFAIK的人来说,它是这样的:
sayHello sh("world");
boost::thread thr(boost::bind(&sayHello::repeatHello,&sh));
【问题讨论】:
标签: multithreading boost bind