std :: forward_list.push_front(std :: thread)无法编译

时间:2014-09-26 21:50:11

标签: c++ multithreading c++11 forward-list

此代码无法编译:

班级声明:

class threadController
{
private:
    static std::forward_list<std::thread>   threadList;
    static std::mutex                       mutexThreadList;

public:
    static void startAndAddThreadToList(int argc, char * argv []);

};

课程定义:

std::forward_list<std::thread>  threadController::threadList;
std::mutex                      threadController::mutexThreadList;

void threadController::startAndAddThreadToList(int argc, char * argv [])
{
    // execute now the thread to avoid a delayed start because of the mutex lock
    std::thread threadInstance(threadCalculateOneInstrument, argc, argv);

    mutexThreadList.lock();
    threadList.push_front(threadInstance);
    mutexThreadList.unlock();
}

这是编译错误:

/usr/local/include/c++/4.9.1/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::thread; _Args = {const std::thread&}; _Tp = std::thread]’:
/usr/local/include/c++/4.9.1/bits/alloc_traits.h:253:4:   required from ‘static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {const std::thread&}; _Alloc = std::allocator<std::thread>; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]’
/usr/local/include/c++/4.9.1/bits/alloc_traits.h:399:57:   required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {const std::thread&}; _Alloc = std::allocator<std::thread>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/local/include/c++/4.9.1/bits/forward_list.h:357:42:   required from ‘std::_Fwd_list_base<_Tp, _Alloc>::_Node* std::_Fwd_list_base<_Tp, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::thread&}; _Tp = std::thread; _Alloc = std::allocator<std::thread>; std::_Fwd_list_base<_Tp, _Alloc>::_Node = std::_Fwd_list_node<std::thread>]’
/usr/local/include/c++/4.9.1/bits/forward_list.tcc:71:64:   required from ‘std::_Fwd_list_node_base* std::_Fwd_list_base<_Tp, _Alloc>::_M_insert_after(std::_Fwd_list_base<_Tp, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::thread&}; _Tp = std::thread; _Alloc = std::allocator<std::thread>; std::_Fwd_list_base<_Tp, _Alloc>::const_iterator = std::_Fwd_list_const_iterator<std::thread>]’
/usr/local/include/c++/4.9.1/bits/forward_list.h:803:9:   required from ‘void std::forward_list<_Tp, _Alloc>::push_front(const _Tp&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>]’
../source/threadController.cpp:27:38:   required from here
/usr/local/include/c++/4.9.1/ext/new_allocator.h:120:4: error: use of deleted function ‘std::thread::thread(const std::thread&)’

我的目的是实现一个线程列表。线程以无限期的执行时间运行。 “控制”线程每隔10秒检查一次列表中的线程。如果线程完成,则为此线程调用thread函数detach()以释放资源。

2 个答案:

答案 0 :(得分:9)

不允许复制std::thread个对象,您可以像这样使用emplace

threadList.emplace_front(threadCalculateOneInstrument, argc, argv);

请参阅: std::forward_list::emplace_front()

答案 1 :(得分:2)

std::threads不可复制,您可以按照Galik的建议使用emplace_front(),也可以插入rvalues(在这种情况下,将调用移动构造函数):

threadList.push_front(std::thread(threadCalculateOneInstrument, argc, argv));

如果您有一个返回std::thread的函数,这可能很有用,因此emplace_front()不适用:

std::thread createThread() {....}
threadList.push_front(createThread());