无法初始化线程矢量



我已经实现了这个简单的 C++11 阻塞队列,我想测试它。为了测试它,我分别使用 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

更新:似乎向量初始化不喜欢通过引用捕获引用的变量,即队列、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;
    });

最新更新