2011-01-19 96 views
0

我試着用參數調用函數,使用boost,但它不起作用。代碼是這樣的將函數參數綁定到線程

void Simulate::cr_number_threaded(lint nodes) { 
    for(lint i = 0; i < trials_per_thread; i++) { 
     // code 
    } 

} 

void Simulate::run_cr_number() { 
    vec_cr_number.clear(); 
    boost::thread t[threads]; 

    for(int i = 0; i < n_s.size(); i++) { 
     // n_s[i] is the current number of nodes 
     for(int t_idx = 0; t_idx < threads; t_idx++) 
      t[t_idx] = boost::thread(cr_number_threaded, n_s[i]); 
     // etc... 
    } 


} 

我得到的錯誤是:

Simulate.cpp: In member function 'void Simulate::run_cr_number()': Simulate.cpp:27: error: no matching function for call to 'boost::thread::thread(, long int&)'

更新: 我跟着建議。使用第一個解決方案,我用第二個獲得

Simulate.cpp: In member function 'void Simulate::run_cr_number()': Simulate.cpp:28: error: no matching function for call to 'bind(, long int&)' ../../boost_1_44_0/boost/bind/bind.hpp:1472: note: candidates are: boost::_bi::bind_t::type> boost::bind(F, A1) [with F = void (Simulate::*)(lint), A1 = long int] ../../boost_1_44_0/boost/bind/bind.hpp:1728: note:
boost::_bi::bind_t::type, boost::_mfi::dm, typename boost::_bi::list_av_1::type> boost::bind(M T::*, A1) [with A1 = long int, M = void()(lint), T = Simulate]

我得到這個代替

Simulate.cpp: In member function 'void Simulate::run_cr_number()': Simulate.cpp:28: error: no matching function for call to 'boost::thread::swap(boost::_bi::bind_t, boost::_bi::list2, boost::_bi::value > >)' ../../boost_1_44_0/boost/thread/detail/thread.hpp:310: note: candidates are: void boost::thread::swap(boost::thread&)

回答

1

1)的boost ::線程不是copyableswappable

2)你需要指定成員功能並通過實例

這樣的事情:

t[t_idx].swap(boost::thread(&Simulate::cr_number_threaded, this, n_s[i])); 

在這種情況下,您需要確保this的壽命長於線程。

+1

`cr_number_threaded`是一個成員函數,所以你需要傳遞一個實例並完全限定它的名字:`boost :: bind(&Simulate :: cr_number_threaded,this,n_s [i])``。 – 2011-01-19 00:58:57