无法初始化线程向量

时间:2016-05-20 09:48:38

标签: c++ multithreading c++11 concurrency

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

#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

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

1 个答案:

答案 0 :(得分:4)

as.Date(mydata$censor_date, "%d%b%Y")

std::vector的构造函数将复制std::thread 10次,但您无法复制// 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::emplace_back

std::thread

我认为有一种方法可以在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);
}