std::atomic_flag 停止多个线程

std::atomic_flag to stop multiple threads

本文关键字:线程 flag atomic std      更新时间:2023-10-16

我正在尝试使用 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));
    }
}