【问题标题】:Unable to initialize threads vector无法初始化线程向量
【发布时间】:2016-05-20 09:48:37
【问题描述】:

我已经实现了我想测试的这个简单的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

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

【问题讨论】:

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


    【解决方案1】:
    // 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;
        });
    

    【讨论】:

    • 不错的答案!谢谢你!但是,我会更乐意进行更直接的初始化而不是循环。有没有更优雅的方法来解决这个用例并避免循环???
    • @GiovanniAzua 你可以试试std::generatestd::back_inserter 或类似的东西。
    • @Angew 我认为这行不通,因为std::generate 使用operator= 分配值
    • @GiovanniAzua 我会继续看,但我目前没有找到一个没有循环的
    • @Angew 但std::generate 不会移动它们,它会复制它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2017-05-14
    • 2020-03-25
    • 2021-06-17
    • 2011-03-04
    • 2017-04-06
    相关资源
    最近更新 更多