У меня есть класс ниже, и это заголовочный файл. Когда я включаю строку std::vector<std::thread> threads;
в ProducerManager.h
, я получаю сообщение об ошибке, показанное внизу этого вопроса. Я прошел через несколько SO вопросов, где проблема не заключалась в использовании std :: move () для перемещения потока в вектор, однако, очевидно, поскольку я делаю это с помощью rvalue, это должно работать. Но я получаю эту странную ошибку, которую я не понимаю. Кто-нибудь может быть так любезен, чтобы помочь мне?
// ProducerManager.h
#include <thread>
#include <queue>
#include "Producer.h"
class ProducerManager {
public:
ProducerManager(std::queue<std::string> *buffer, Semaphore *items);
int run();
private:
std::queue<std::string> *buffer;
Semaphore *items;
bool empty_page_reached = false;
unsigned int max_threads = 50;
unsigned int num_pages = 0;
std::vector<std::thread> threads; // If I remove this line, I no longer get the error
std::vector<Producer*> producers;
std::queue<int> pids;
};
// ProducerManager.cc
#include "ProducerManager.h"
ProducerManager::ProducerManager(std::queue<std::string> *buffer, Semaphore *items) {
this->buffer = buffer;
this->items = items;
}
int ProducerManager::run(void) {
...
}
// main.cc
#include <string>
#include <thread>
#include "ProducerManager.h"
#include "Consumer.h"
#include "Semaphore.h"
int main(int argc, char *argv[]) {
std::queue<std::string> *buffer = new std::queue<std::string>();
Semaphore *items = new Semaphore(0);
// Testing with just one consumer
Consumer *c1 = new Consumer(buffer, items);
std::thread cth1(&Consumer::perform, c1);
ProducerManager pm(buffer, items);
std::thread pm_thread(&ProducerManager::run, pm);
pm_thread.join();
cth1.join();
delete c1;
return 0;
}
Ошибка:
/usr/include/c++/9.2.1/bits/stl_uninitialized.h: In instantiation of ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*]’:
/usr/include/c++/9.2.1/bits/stl_uninitialized.h:307:37: required from ‘_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]’
/usr/include/c++/9.2.1/bits/stl_vector.h:555:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>]’ProducerManager.h:6:7: required from ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = ProducerManager&; long unsigned int _Idx = 1; _Head = ProducerManager]’/usr/include/c++/9.2.1/tuple:349:38: required from ‘static 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::*)()]’/usr/include/c++/9.2.1/thread:131:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ProducerManager::*)(); _Args = {ProducerManager&}; <template-parameter-1-3> = void]’main.cc:17:50: required from here
/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,