2016-07-25 126 views
-3

我有,我想在一個std ::線程運行的類成員函數,它需要一個函數作爲參數,如下所示:如何啓動一個std ::線程調用一個需要函數作爲參數的函數?

ModbusAgent::poolingThread(int position, std::function<int(int, uint16_t*)> readFunction) 

因此問題是:我怎麼能實例化一個std: :調用該線程?

+1

你不覺得這個問題你提出並回答自己是最適合於[SO文檔(http://stackoverflow.com /文檔)? – WhiZTiM

+0

函數參數有什麼特別之處?這只是另一個參數。 – Barry

+0

使用lambda會更容易 – nishantjr

回答

-2

因此,這裏是我發現這樣做的方式:

class A { 
public: 
    int bar(int, uint16_t*) { return 0; } 

    void foo(int position, std::function<int(int, uint16_t*)> readFunction) { 
      readFunction(position, nullptr); 
    } 

    void b() { 
     std::function<int(int, uint16_t*)> readFunction = std::bind(&A::bar, *this, std::placeholders::_1, std::placeholders::_2); 
     std::thread(&A::foo, this, 5, readFunction); 
    } 
}; 


int main() { 
    A a; 
    a.b(); 
} 
相關問題