尝试使用 std::vector<std::thread> 时出现静态断言失败错误

Mat*_*ugh 3 c++ multithreading c++11

我有下面的类及其头文件。std::vector<std::thread> threads;当我在 中包含该行时ProducerManager.h,我收到此问题底部显示的错误。我已经解决了多个 SO 问题,其中问题不是使用 std::move() 将线程移动到向量中,但是,显然因为我是通过右值来做的,所以它应该可以工作。但我遇到了这个我不明白的奇怪错误。有人可以好心帮助我吗?

\n\n
// ProducerManager.h\n\n#include <thread>\n#include <queue>\n\n#include "Producer.h"\n\nclass ProducerManager {\n  public:\n    ProducerManager(std::queue<std::string> *buffer, Semaphore *items);\n    int run();\n  private:\n    std::queue<std::string> *buffer;\n    Semaphore *items;\n    bool empty_page_reached = false;\n    unsigned int max_threads = 50;\n    unsigned int num_pages = 0;\n    std::vector<std::thread> threads;   // If I remove this line, I no longer get the error\n    std::vector<Producer*> producers;\n    std::queue<int> pids;\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
// ProducerManager.cc\n\n#include "ProducerManager.h"\n\nProducerManager::ProducerManager(std::queue<std::string> *buffer, Semaphore *items) {\n  this->buffer = buffer;\n  this->items = items;\n}\n\nint ProducerManager::run(void) {\n  ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
// main.cc\n\n#include <string>\n#include <thread>\n\n#include "ProducerManager.h"\n#include "Consumer.h"\n#include "Semaphore.h"\n\nint main(int argc, char *argv[]) {\n  std::queue<std::string> *buffer = new std::queue<std::string>();\n  Semaphore *items = new Semaphore(0);\n\n  // Testing with just one consumer\n  Consumer *c1 = new Consumer(buffer, items);\n  std::thread cth1(&Consumer::perform, c1);\n\n  ProducerManager pm(buffer, items);\n  std::thread pm_thread(&ProducerManager::run, pm);\n\n  pm_thread.join();\n  cth1.join();\n\n  delete c1;\n\n  return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误:

\n\n
/usr/include/c++/9.2.1/bits/stl_uninitialized.h: In instantiation of \xe2\x80\x98_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*]\xe2\x80\x99:\n/usr/include/c++/9.2.1/bits/stl_uninitialized.h:307:37:   required from \xe2\x80\x98_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*; _Tp = std::thread]\xe2\x80\x99\n/usr/include/c++/9.2.1/bits/stl_vector.h:555:31:   required from \xe2\x80\x98std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>]\xe2\x80\x99ProducerManager.h:6:7:   required from \xe2\x80\x98constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = ProducerManager&; long unsigned int _Idx = 1; _Head = ProducerManager]\xe2\x80\x99/usr/include/c++/9.2.1/tuple:349:38:   required from \xe2\x80\x98static std::thread::_Invoker<std::tuple<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...> > std::thread::__make_invoker(_Callable&&, _Args&& ...) [with _Callable = int (ProducerManager::*)(); _Args = {ProducerManager&}; typename std::decay<_Tp>::type = int (ProducerManager::*)()]\xe2\x80\x99/usr/include/c++/9.2.1/thread:131:22:   required from \xe2\x80\x98std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ProducerManager::*)(); _Args = {ProducerManager&}; <template-parameter-1-3> = void]\xe2\x80\x99main.cc:17:50:   required from here\n/usr/include/c++/9.2.1/bits/stl_uninitialized.h:127:72: error: static assertion failed: result type must be constructible from value type of input range  127 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,\n
Run Code Online (Sandbox Code Playgroud)\n

Igo*_*nik 5

做了

std::thread pm_thread(&ProducerManager::run, std::ref(pm));
// or
std::thread pm_thread(&ProducerManager::run, &pm);
Run Code Online (Sandbox Code Playgroud)

std::thread的构造函数按值获取参数。正如最初所写,它尝试复制pm,但ProducerManager不可复制(因为它的成员std::vector<std::thread> threads不可复制,因为std::thread不可复制)。