std::forward_list.push_front(std::thread) 编译失败

std::forward_list.push_front(std::thread) fails to compile

本文关键字:std 失败 编译 thread push forward list front      更新时间:2023-10-16

这段代码编译失败:

类声明:

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()则为此线程调用该函数以释放资源。

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

threadList.emplace_front(threadCalculateOneInstrument, argc, argv);

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

std::threads不可

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

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

如果你有一个返回 std::thread 的函数,这会很有用,所以emplace_front()不适用:

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