std::atomic_flag 停止多个线程
std::atomic_flag to stop multiple threads
我正在尝试使用 std::atomic_flag
停止多个工作线程。从使用 std::atomic_flag 和工作线程的问题开始,以下工作:
#include <iostream>
#include <atomic>
#include <chrono>
#include <thread>
std::atomic_flag continueFlag;
std::thread t;
void work()
{
while (continueFlag.test_and_set(std::memory_order_relaxed)) {
std::cout << "work ";
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void start()
{
continueFlag.test_and_set(std::memory_order_relaxed);
t = std::thread(&work);
}
void stop()
{
continueFlag.clear(std::memory_order_relaxed);
t.join();
}
int main()
{
std::cout << "Start" << std::endl;
start();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::cout << "Stop" << std::endl;
stop();
std::cout << "Stopped." << std::endl;
return 0;
}
尝试重写为多个工作线程:
#include <iostream>
#include <atomic>
#include <chrono>
#include <thread>
#include <vector>
#include <memory>
struct thread_data {
std::atomic_flag continueFlag;
std::thread thread;
};
std::vector<thread_data> threads;
void work(int threadNum, std::atomic_flag &continueFlag)
{
while (continueFlag.test_and_set(std::memory_order_relaxed)) {
std::cout << "work" << threadNum << " ";
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void start()
{
const unsigned int numThreads = 2;
for (int i = 0; i < numThreads; i++) {
////////////////////////////////////////////////////////////////////
//PROBLEM SECTOR
////////////////////////////////////////////////////////////////////
thread_data td;
td.continueFlag.test_and_set(std::memory_order_relaxed);
td.thread = std::thread(&work, i, td.continueFlag);
threads.push_back(std::move(td));
////////////////////////////////////////////////////////////////////
//PROBLEM SECTOR
////////////////////////////////////////////////////////////////////
}
}
void stop()
{
//Flag stop
for (auto &data : threads) {
data.continueFlag.clear(std::memory_order_relaxed);
}
//Join
for (auto &data : threads) {
data.thread.join();
}
threads.clear();
}
int main()
{
std::cout << "Start" << std::endl;
start();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::cout << "Stop" << std::endl;
stop();
std::cout << "Stopped." << std::endl;
return 0;
}
我的问题是上面的"问题部门"。即创建线程。我无法思考如何实例化线程并将变量传递给工作线程。
现在的错误是引用此行threads.push_back(std::move(td));
错误Error C2280 'thread_data::thread_data(const thread_data &)': attempting to reference a deleted function
。
尝试使用这样的unique_ptr:
auto td = std::make_unique<thread_data>();
td->continueFlag.test_and_set(std::memory_order_relaxed);
td->thread = std::thread(&work, i, td->continueFlag);
threads.push_back(std::move(td));
在第 td->thread = std::thread(&work, i, td->continueFlag);
行给出错误std::atomic_flag::atomic_flag(const std::atomic_flag &)': attempting to reference a deleted function
。我是否从根本上误解了 std::atomic_flag 的使用?它真的既不可移动又不可复制吗?
你的第一种方法实际上更接近事实。问题在于它将对本地for
循环作用域内对象的引用作为参数传递给每个线程。但是,当然,一旦循环迭代结束,该对象就会超出范围并被销毁,每个线程都引用一个被销毁的对象,从而导致未定义的行为。
没有人关心你在创建线程后将对象移动到std::vector
中的事实。线程接收到对本地范围对象的引用,这就是它所知道的全部内容。故事结束。
首先将对象移动到向量中,然后将对std::vector
中对象的引用传递给每个线程也不起作用。一旦载体在内部重新分配,作为其自然生长的一部分,你就会陷入同样的困境。
需要做的是在实际启动任何std::thread
之前先创建整个threads
数组。如果虔诚地遵循RAII原则,那只不过是对std::vector::resize()
的简单呼吁。
然后,在第二个循环中,遍历完全熟threads
数组,并为数组中的每个元素生成一个std::thread
。
我几乎带着我的unique_ptr
解决方案在那里。我只需要将调用作为 std::ref() 传递如下:
std::vector<std::unique_ptr<thread_data>> threads;
void start()
{
const unsigned int numThreads = 2;
for (int i = 0; i < numThreads; i++) {
auto td = std::make_unique<thread_data>();
td->continueFlag.test_and_set(std::memory_order_relaxed);
td->thread = std::thread(&work, i, std::ref(td->continueFlag));
threads.push_back(std::move(td));
}
}
然而,受到上面山姆的启发,我也想出了一种非指针方式:
std::vector<thread_data> threads;
void start()
{
const unsigned int numThreads = 2;
//create new vector, resize doesn't work as it tries to assign/copy which atomic_flag
//does not support
threads = std::vector<thread_data>(numThreads);
for (int i = 0; i < numThreads; i++) {
auto& t = threads.at(i);
t.continueFlag.test_and_set(std::memory_order_relaxed);
t.thread = std::thread(&work, i, std::ref(t.continueFlag));
}
}
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 为什么我不能在 while 循环中创建线程?