独立 std::threads 的 C++ std::vector

标签 c++ multithreading c++11 vector stdthread

我正在构建一个实时软件,其中我在 main() 上有一个主无限循环和用于读取和处理数据的线程。

其中一个问题是保持运行线程的 std::vector 向它们发送信号并监视执行。所以我把这段代码放在一起:

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <chrono>

namespace readerThread {

    void start(int id)
    {
        while (1)
        {
            std::cout << "Reader " << id << " running..." <<  std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        }
    }

}


int main() 
{

        int readers[] = { 1, 2, 3 };
        
        std::vector<std::thread> readerThreads;

        for (int &reader : readers)
        {
            std::thread th(readerThread::start, reader);
            readerThreads.push_back(th);
        }

        while(true)
        {
            std::cout << "Waiting..." << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(10000));
        }
        
        return 0;
}

它甚至没有编译,得到这个错误:

In file included from /usr/local/include/c++/5.1.0/x86_64-unknown-linux-gnu/bits/c++allocator.h:33:0,
                 from /usr/local/include/c++/5.1.0/bits/allocator.h:46,
                 from /usr/local/include/c++/5.1.0/string:41,
                 from /usr/local/include/c++/5.1.0/bits/locale_classes.h:40,
                 from /usr/local/include/c++/5.1.0/bits/ios_base.h:41,
                 from /usr/local/include/c++/5.1.0/ios:42,
                 from /usr/local/include/c++/5.1.0/ostream:38,
                 from /usr/local/include/c++/5.1.0/iostream:39,
                 from main.cpp:1:
/usr/local/include/c++/5.1.0/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::thread; _Args = {const std::thread&}; _Tp = std::thread]':
/usr/local/include/c++/5.1.0/bits/alloc_traits.h:256:4:   required from 'static std::_Require<std::allocator_traits<_Alloc>::__has_construct<_Tp, _Args ...> > std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {const std::thread&}; _Alloc = std::allocator<std::thread>; std::_Require<std::allocator_traits<_Alloc>::__has_construct<_Tp, _Args ...> > = void]'
/usr/local/include/c++/5.1.0/bits/alloc_traits.h:402:16:   required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {const std::thread&}; _Alloc = std::allocator<std::thread>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]'
/usr/local/include/c++/5.1.0/bits/stl_vector.h:917:30:   required from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>; std::vector<_Tp, _Alloc>::value_type = std::thread]'
main.cpp:37:30:   required from here
/usr/local/include/c++/5.1.0/ext/new_allocator.h:120:4: error: use of deleted function 'std::thread::thread(const std::thread&)'
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^
In file included from main.cpp:4:0:
/usr/local/include/c++/5.1.0/thread:126:5: note: declared here
     thread(const thread&) = delete;
     ^

线程是独立的,所以我不需要在主程序或任何线程上调用join...

所以,这是我的疑惑:

为什么我的代码无法编译?

这是存储线程 vector 的正确方法吗?

感谢您的帮助...

附言:Original code here :

最佳答案

你需要使用类似的东西

readerThreads.push_back(move(th));

这将使 th 成为右值,并调用移动构造函数。 thread 的复制构造函数是 disabled通过设计(参见 Anthony Williams 的 C++ Concurrency In Action )。

关于独立 std::threads 的 C++ std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768216/

相关文章:

c++11 - 是所有数据成员都初始化为 0 还是由自动调用的构造函数分配随机值?

c++ - 如何在对象创建时从 QML 调用任意 C++ 函数?

c++公共(public)对象更改应该调用函数

c++ - MySQL C++ 连接器 : How do I get the thread/connection Id?

c++ - 二维数组的行为

c - 多线程程序 : waiting on input

c++ - 使用 boost 序列化到磁盘后无法加载回数据

c++ - Variadic template 选择更通用的模板而不是重载

java - 轮询是否会阻塞 LinkedBlockingQueue 中的其他操作?

java - 多线程服务器如何工作?