gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:35:55 26 4
gpt4 key购买 nike

此代码编译失败:

类声明:

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() .

关于c++ - std::forward_list.push_front(std::thread) 编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com