gpt4 book ai didi

c++ - 无法初始化线程 vector

转载 作者:搜寻专家 更新时间:2023-10-31 00:57:29 24 4
gpt4 key购买 nike

我已经实现了这个简单的 C++11 Blocking Queue我想测试一下。为了测试它,我分别用 10 个生产者和 10 个消费者初始化了以下生产者和消费者线程 vector 。

#include <iostream>
#include <random>
#include <thread>
#include <vector>

#include "blockingqueue.h"

int main() {
// create a blocking queue with capacity 3
blocking_queue<int> queue(3);

uniform_int_distribution<> dis(1, 10);
random_device rd;
mt19937 gen(rd());

// create 10 producers
vector<thread> producers(10, thread([&] () {
cout << "attempting to produce a job ..." << endl;
int job = dis(gen);
queue.put(job);
cout << "produced job " << job << endl;
}));

// create 10 consumers
vector<thread> consumers(10, thread([&] () {
cout << "attempting to take a job ..." << endl;
int job = queue.take();
cout << "consumed job " << job << endl;
}));

// wait for all producers to complete
for(auto& thread : producers){
thread.join();
}

// wait for all consumers to complete
for(auto& thread : consumers){
thread.join();
}

return EXIT_SUCCESS;
}

但是,我无法编译它并出现以下错误:

g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/cpp11showcase.d" -MT"src/cpp11showcase.o" -o "src/cpp11showcase.o" "../src/cpp11showcase.cpp"
In file included from /usr/include/c++/4.8/vector:62:0,
from ../src/cpp11showcase.cpp:13:
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::thread; _Args = {const std::thread&}]’:
/usr/include/c++/4.8/bits/stl_uninitialized.h:187:48: required from ‘static void std::__uninitialized_fill_n<_TrivialValueType>::__uninit_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = std::thread*; _Size = long unsigned int; _Tp = std::thread; bool _TrivialValueType = false]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:224:35: required from ‘void std::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = std::thread*; _Size = long unsigned int; _Tp = std::thread]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:334:50: required from ‘void std::__uninitialized_fill_n_a(_ForwardIterator, _Size, const _Tp&, std::allocator<_Tp2>&) [with _ForwardIterator = std::thread*; _Size = long unsigned int; _Tp = std::thread; _Tp2 = std::thread]’
/usr/include/c++/4.8/bits/stl_vector.h:1215:32: required from ‘void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::thread]’
/usr/include/c++/4.8/bits/stl_vector.h:284:40: required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::thread; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::thread>]’
../src/cpp11showcase.cpp:83:4: required from here
/usr/include/c++/4.8/bits/stl_construct.h:75:7: error: use of deleted function ‘std::thread::thread(const std::thread&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from ../src/blockingqueue.h:13:0,
from ../src/cpp11showcase.cpp:18:
/usr/include/c++/4.8/thread:126:5: error: declared here
thread(const thread&) = delete;
^
make: *** [src/cpp11showcase.o] Error 1

更新:似乎 vector 初始化不喜欢通过引用捕获引用变量,即队列、dis 和 gen。线程复制构造函数有方法thread(const thread&) = delete;不可用

最佳答案

// create 10 producers
vector<thread> producers(10, thread([&] () {
cout << "attempting to produce a job ..." << endl;
int job = dis(gen);
queue.put(job);
cout << "produced job " << job << endl;
}));

std::vector 的构造函数将复制 std::thread 10 次,但是你不能复制 std::thread,因为它的复制构造函数被删除了。

相反,您可以使用 std::vector::emplace_back :

vector<thread> producers;
//Loop 10 times (for 10 threads)
for (std::size_t i = 0; i < 10; ++i)
produces.emplace_back([&] () {
cout << "attempting to take a job ..." << endl;
int job = queue.take();
cout << "consumed job " << job << endl;
});

我认为没有办法在 std::vector 中就地构造 n 元素,但您可以将循环放在函数中:

template<typename T>
void fillThread(std::vector<std::thread>& threads, std::size_t count, T func)
{
for (std::size_t i = 0; i < count; ++i)
threads.emplace_back(func);
}

然后你可以这样调用它:

std::vector<std::thread> producers;

fillThread(producers, 10, [&] () {
cout << "attempting to take a job ..." << endl;
int job = queue.take();
cout << "consumed job " << job << endl;
});

关于c++ - 无法初始化线程 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37343457/

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