0

我有一个带有对象指针的向量,我试图在一个新线程中启动对象的方法(方法有一个参数)。

这是我无法编译的代码:

class CanaSynchDynamic {
...
    void start() (boost::barrier&);
...
};

主要是:

for(int i=0;i<pw;++i)
    vS1.push_back(new CanaSynchDynamic());

do {
        boost::barrier barrier(pw);
        boost::thread_group threads;
        for(int i=0;i<pw;++i)
            vS1[i]->more_steps(start,s[z]);
        for(int i=0;i<pw;++i)
            threads.create_thread(boost::bind(&CanaSynchDynamic::start,boost::ref(*(vS1[i])),boost::ref(barrier)));
        threads.join_all();

} while(something);

错误是:

    /usr/include/boost/thread/detail/thread.hpp: In instantiation of 'void
    boost::detail::thread_data<boost::reference_wrapper<T> >::run() [with F = CanaSynchDynamic]':
    simulation_3.cpp:278:1:   required from here
    /usr/include/boost/thread/detail/thread.hpp:98:17: error: no match for call to   '(CanaSynchDynamic) ()'

你有什么主意吗?

4

1 回答 1

2

You can't use a reference_wrapper to pass the object from which the function shall be run. Instead you can just pass a pointer to your object:

threads.create_thread(boost::bind(&CanaSynchDynamic::start,vS1[i],boost::ref(barrier)));

Also, you might just store your objects in a vector and not pointers to them. If you need a pointer use a std::unique_ptr from C++11 or if that is not available maybe a boost::ptr_vector.

于 2013-03-10T16:26:09.937 回答