2011-11-28 61 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可能是非常有益的。