2011-11-28 32 views
5

我想有创建时,一个类,启动一个后台线程,类似于下面:的boost ::类中的线程

class Test 
{ 
    boost::thread thread_; 
    void Process() 
    { 
    ... 
    } 

    public: 
    Test() 
    { 
     thread_ = boost::thread(Process); 
    } 
} 

我不能得到它来编译,错误是“调用boost :: thread :: thread(未解析函数类型)时没有匹配函数”。当我在课外做这件事时,它工作正常。我怎样才能让函数指针起作用?

回答

6

你应该初始化thread_为:

Test() 
    : thread_(<initialization here, see below>) 
{ 
} 

ProcessTest类的成员非静态方法。您可以:

  • 声明Process为静态。
  • 绑定测试实例以调用Process

如果声明Process为静态,初始化应该只是

&Test::Process 

否则,您可以使用绑定Boost.Bind的Test一个实例:

boost::bind(&Test::Process, this) 
0

让您的加工方法静:

static void Process() 
    { 
    ... 
    } 
4

的问题是,你想用指向成员函数的指针初始化boost :: thread。

你将需要:

Test() 
    :thread_(boost::bind(&Test::Process, this)); 
{ 

} 

而且这question可能是非常有益的。