使用boost调用成员函数作为线程函数

时间:2012-11-23 10:21:40

标签: c++ multithreading boost

我尝试使用以下语句将成员函数作为线程函数调用

boost::thread getURInHashThread(boost::bind(&Worker::run, this));

其中

Worker 

是班级和

run()

是方法。我在同一个Worker类的另一个成员函数中有这个语句所以我给了this

但是,我收到了错误

error:bind is not a member of boost.

我无法理解这一点。请帮忙。 在此先感谢:)。

#include <boost/thread/thread.hpp>
#include <iostream>

class Test
{
 public:
  void Main() { boost::thread t(&Test::run, this); }
  void run() { while(1){  std::cout << "some functionality here"; } }
};

int main()
{
   Test test;
   test.Main();
}

1 个答案:

答案 0 :(得分:0)

boost thread在内部使用bind,所以:

#include <boost/thread/thread.hpp>
#include <iostream>

class Test
{
public:
  void Main() { std::cout << "hello" << std::endl; }
};

int main()
{
  Test test;
  boost::thread t(&Test::Main, test);
  t.join();
}
相关问题