std::forward_list.push_front(std::thread) 编译失败
std::forward_list.push_front(std::thread) fails to compile
这段代码编译失败:
类声明:
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());
相关文章:
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 如果我重新定义 sqrt 函数,为什么使用 std::sqrt 失败?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- C++ std::regex 使用前瞻失败
- 使用用户定义的类型 UDT 实例化 std::atomic<>。如果 UDT 具有虚函数,则 l 墨水将失败。为什么?
- std::copy 导致运行时检查失败 #2
- C++从 std::string 创建 CString 失败
- 为什么这个分配给 std::vector 索引会失败?
- 带有引用的std::tuple在clang中编译失败,但在gcc中编译失败
- 调用以CWinThread为基的类运算符()的std::thread失败
- 使用"std::enable_if"转发包装失败
- 为什么 std::get<T> 其中 T 是调用 constexpr 函数失败的结果?
- 为什么模板参数推导失败,std::函数回调的可变参数模板参数?
- 在 static_assert 中使用 std::is_base_of 由于类型不完整而失败
- 使用"std::function"和先前推断的模板参数替换失败 - 为什么?
- 从 std::vector<double> 到 Python 列表的转换失败(提升 python)
- 卡萨布兰卡test_runner失败,并显示 std::bad_alloc
- Visual Studio 2017 STL 可视化工具失败了一个 std::map<MyIntrusivePtr, std::tuple<....> >