我已经实现了我想测试的这个简单的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;
});
我不认为有一种方法可以在
n
中适本地构造std::vector
元素,但是您可以将循环放入函数中: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;
});