C++std::vector独立std::threads
C++ std::vector of independent std::threads
我正在构建一个实时软件,其中我在 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
...
所以,这是我的疑惑:
为什么我的代码无法编译?
这是存储线程向量的正确方法吗?
感谢您的帮助...
你需要使用像
这样的东西
readerThreads.push_back(move(th));
这将使 th
成为右值,并调用移动构造函数。 thread
的复制构造函数是 disabled by design (see Anthony Williams' C++ Concurrency In Action).
/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)...); }
让我们稍微揭开这个面纱。
error: use of deleted function 'std::thread::thread(const std::thread&)'
您的代码正在尝试引入 std::thread
.
required from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&)
push_back
是罪魁祸首。
std::thread
不可复制 - 意味着什么 复制一个线程?
std::thread t1([](){});
std::thread t2 = t1;
因此 std::thread
对象的实例旨在成为唯一的所有者。除了简单的困惑,随之而来的还有很多痛苦。
然而,它们是可移动的。
std::thread t1([](){});
std::thread t2 = std::move(t1);
t1
不再是有效的线程描述符,它所描述的线程现在属于 t2
。
要将这些东西放入容器中,您可以使用 std::move
或 std::emplace
/std::emplace_back
。
std::vector<std::thread> threads;
threads.push_back(std::move(std::thread([](){})));
threads.emplace_back([](){});
虽然您的代码专注于这个特定问题,但我要指出,C++ 标准将其声明为在线程仍处于附加状态且未连接时调用线程析构函数的错误。
int main() {
std::thread t1([](){ while (true) { std::this_thread::yield(); } };
}
当 main 终止时,调用 t1.~thread() ,它检测到线程仍然附加并且没有加入,这会引发异常导致关闭崩溃。
您需要 join()
线程,等待它终止 运行,或者 detach()
它。如果你想使用join()
,你需要一些方法来告诉线程停止,如果你detach()
程序可能会在线程中间退出,做一些像写数据等的事情,你可以介绍一个严重的错误。
#include <thread>
#include <chrono>
#include <future>
int main () {
std::promise<void> cnx_promise;
std::shared_future<void> cnx_future;
std::thread t1([cnx_future]() {
while (cnx_future.valid()) {
std::this_thread::yield();
}
});
std::this_thread::sleep_for(std::chrono::seconds(1));
cnx_promise.set_value();
t1.join();
}
这里我们使用 promise 让线程知道什么时候该停止 运行,但是您可以使用条件变量、信号等,或者甚至只是您测试的简单 std::atomic<bool> ok_to_run { true };
为假。
另一个可行的变体是在 vector.push_back 调用中创建您的线程对象。在这种情况下不需要调用 std::move 因为它已经是一个右值(因此它将被移动)。
for (int &reader : readers)
readerThreads.push_back(std::thread(readerThread::start, reader));
这是保证每个线程都将在 vector 的存储中创建。不会复制任何对象字节。
for (int &reader : readers)
readerThreads.emplace_back( readerThread::start, reader );
在通常情况下,这与@Seth 建议的不同。但在目前的情况下,它是 99% 相同的。
我正在构建一个实时软件,其中我在 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
...
所以,这是我的疑惑:
为什么我的代码无法编译?
这是存储线程向量的正确方法吗?
感谢您的帮助...
你需要使用像
这样的东西readerThreads.push_back(move(th));
这将使 th
成为右值,并调用移动构造函数。 thread
的复制构造函数是 disabled by design (see Anthony Williams' C++ Concurrency In Action).
/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)...); }
让我们稍微揭开这个面纱。
error: use of deleted function 'std::thread::thread(const std::thread&)'
您的代码正在尝试引入 std::thread
.
required from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&)
push_back
是罪魁祸首。
std::thread
不可复制 - 意味着什么 复制一个线程?
std::thread t1([](){});
std::thread t2 = t1;
因此 std::thread
对象的实例旨在成为唯一的所有者。除了简单的困惑,随之而来的还有很多痛苦。
然而,它们是可移动的。
std::thread t1([](){});
std::thread t2 = std::move(t1);
t1
不再是有效的线程描述符,它所描述的线程现在属于 t2
。
要将这些东西放入容器中,您可以使用 std::move
或 std::emplace
/std::emplace_back
。
std::vector<std::thread> threads;
threads.push_back(std::move(std::thread([](){})));
threads.emplace_back([](){});
虽然您的代码专注于这个特定问题,但我要指出,C++ 标准将其声明为在线程仍处于附加状态且未连接时调用线程析构函数的错误。
int main() {
std::thread t1([](){ while (true) { std::this_thread::yield(); } };
}
当 main 终止时,调用 t1.~thread() ,它检测到线程仍然附加并且没有加入,这会引发异常导致关闭崩溃。
您需要 join()
线程,等待它终止 运行,或者 detach()
它。如果你想使用join()
,你需要一些方法来告诉线程停止,如果你detach()
程序可能会在线程中间退出,做一些像写数据等的事情,你可以介绍一个严重的错误。
#include <thread>
#include <chrono>
#include <future>
int main () {
std::promise<void> cnx_promise;
std::shared_future<void> cnx_future;
std::thread t1([cnx_future]() {
while (cnx_future.valid()) {
std::this_thread::yield();
}
});
std::this_thread::sleep_for(std::chrono::seconds(1));
cnx_promise.set_value();
t1.join();
}
这里我们使用 promise 让线程知道什么时候该停止 运行,但是您可以使用条件变量、信号等,或者甚至只是您测试的简单 std::atomic<bool> ok_to_run { true };
为假。
另一个可行的变体是在 vector.push_back 调用中创建您的线程对象。在这种情况下不需要调用 std::move 因为它已经是一个右值(因此它将被移动)。
for (int &reader : readers)
readerThreads.push_back(std::thread(readerThread::start, reader));
这是保证每个线程都将在 vector 的存储中创建。不会复制任何对象字节。
for (int &reader : readers)
readerThreads.emplace_back( readerThread::start, reader );
在通常情况下,这与@Seth 建议的不同。但在目前的情况下,它是 99% 相同的。