• <bdo id='x17tp'></bdo><ul id='x17tp'></ul>
      <legend id='x17tp'><style id='x17tp'><dir id='x17tp'><q id='x17tp'></q></dir></style></legend>
      <i id='x17tp'><tr id='x17tp'><dt id='x17tp'><q id='x17tp'><span id='x17tp'><b id='x17tp'><form id='x17tp'><ins id='x17tp'></ins><ul id='x17tp'></ul><sub id='x17tp'></sub></form><legend id='x17tp'></legend><bdo id='x17tp'><pre id='x17tp'><center id='x17tp'></center></pre></bdo></b><th id='x17tp'></th></span></q></dt></tr></i><div id='x17tp'><tfoot id='x17tp'></tfoot><dl id='x17tp'><fieldset id='x17tp'></fieldset></dl></div>

        <small id='x17tp'></small><noframes id='x17tp'>

        <tfoot id='x17tp'></tfoot>

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

        时间:2023-10-07

          <small id='hzxIr'></small><noframes id='hzxIr'>

          <legend id='hzxIr'><style id='hzxIr'><dir id='hzxIr'><q id='hzxIr'></q></dir></style></legend>
            <tbody id='hzxIr'></tbody>

          <tfoot id='hzxIr'></tfoot>
                • <bdo id='hzxIr'></bdo><ul id='hzxIr'></ul>

                  <i id='hzxIr'><tr id='hzxIr'><dt id='hzxIr'><q id='hzxIr'><span id='hzxIr'><b id='hzxIr'><form id='hzxIr'><ins id='hzxIr'></ins><ul id='hzxIr'></ul><sub id='hzxIr'></sub></form><legend id='hzxIr'></legend><bdo id='hzxIr'><pre id='hzxIr'><center id='hzxIr'></center></pre></bdo></b><th id='hzxIr'></th></span></q></dt></tr></i><div id='hzxIr'><tfoot id='hzxIr'></tfoot><dl id='hzxIr'><fieldset id='hzxIr'></fieldset></dl></div>
                  本文介绍了C++ std::vector 的独立 std::threads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I´m building a real time software where I have a main infinite loops on main() and threads used to read and process data.

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

                  One of the issues is keeping a std::vector of running threads to send signals to them and to monitor execution. So I put together this code:

                  #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;
                  }
                  

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

                  It doesn´t even compile, getting this error:

                  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...

                  The threads areindependent, so I don´t need to call join on the main program nor on any thread...

                  所以,这是我的疑问:

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

                  Why my code does not compile?

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

                  Is that the correct way to store the vector of threads ?

                  感谢您的帮助...

                  PS:原代码:

                  推荐答案

                  你需要使用类似的东西

                  readerThreads.push_back(move(th));
                  

                  这将使 th 成为右值,并导致调用移动构造函数.thread 的复制构造函数被设计为禁用(请参阅 Anthony Williams 的 C++ 并发实战).

                  This will make th an rvalue, and cause the move ctor to be called. The copy ctor of thread was disabled by design (see Anthony Williams' C++ Concurrency In Action).

                  这篇关于C++ std::vector 的独立 std::threads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::vector 元素的销毁顺序 下一篇:根据两个值对 STL 向量进行排序

                  相关文章

                  最新文章

                  <small id='dzt8W'></small><noframes id='dzt8W'>

                      • <bdo id='dzt8W'></bdo><ul id='dzt8W'></ul>
                      <legend id='dzt8W'><style id='dzt8W'><dir id='dzt8W'><q id='dzt8W'></q></dir></style></legend>
                      <tfoot id='dzt8W'></tfoot>

                    1. <i id='dzt8W'><tr id='dzt8W'><dt id='dzt8W'><q id='dzt8W'><span id='dzt8W'><b id='dzt8W'><form id='dzt8W'><ins id='dzt8W'></ins><ul id='dzt8W'></ul><sub id='dzt8W'></sub></form><legend id='dzt8W'></legend><bdo id='dzt8W'><pre id='dzt8W'><center id='dzt8W'></center></pre></bdo></b><th id='dzt8W'></th></span></q></dt></tr></i><div id='dzt8W'><tfoot id='dzt8W'></tfoot><dl id='dzt8W'><fieldset id='dzt8W'></fieldset></dl></div>