开发者

bind function parameters to thread

开发者 https://www.devze.com 2023-02-05 04:21 出处:网络
I tried calling a function with parameters, using boost, but it does not work. the code is this void Simulate::cr_number_threaded(lint nodes) {

I tried calling a function with parameters, using boost, but it does not work. the code is this

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...
    }


}

the error I get is the following:

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&)'

UPDATE: I followed the suggestions. Using the first solution I get

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]

using the second one I get this instead

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) boost::thread is not copyable but swappable

2) you need to specify member function and pass an instance

something like this:

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

in this case you need to be sure that this will live longer than threads.

0

精彩评论

暂无评论...
验证码 换一张
取 消