c++ - 如何在C++中加入多个不停止的线程

标签 c++ multithreading stdthread

在 C++ 中,我有一个线程 std::vector,每个线程运行一个永远运行的函数 [while(true)]。 我将它们加入到 for 循环中:

for (auto& thread : threads) 
{
    thread.join();
}

当程序完成时,我在其中一个线程的析构函数内收到 std::terminate() 调用。我想我明白为什么会发生这种情况,除了第一个线程之外,其他连接调用都不会被调用。

连接这些线程的正确方法是什么? 真的有必要加入他们吗? (假设他们在正常情况下不应该加入)

最佳答案

如果线程因为永远不会退出而无法连接,那么您可以使用 std::thread::detach ( https://en.cppreference.com/w/cpp/thread/thread/detach )。无论哪种方式,在加入之前,您都应该始终检查 std::thread::joinable ( https://en.cppreference.com/w/cpp/thread/thread/joinable )。

std::terminate 确实最有可能是由于正在运行的线程被销毁并且在此之前没有被分离或加入。但请注意,应用程序退出时分离线程发生的情况是实现定义的。如果可能的话,您应该更改这些线程中的逻辑以允许正常退出(std::jthreadstd::atomic 可以帮助使线程可停止):

编辑: 半完整的 C++17“正确”代码:

std::atomic stop{false};
std::vector<std::thread> threads;
threads.emplace_back(std::thread{[&] { while (!stop.load()) { /* */ }}});
threads.emplace_back(std::thread{[&] { while (!stop.load()) { /* */ }}});

//...

stop.store(true);

for (auto& thread : threads) 
{
    if (thread.joinable())
    {
        thread.join();
    }
}

半完整的 C++20“正确”代码:

std::vector<std::jthread> threads;
threads.emplace_back(std::jthread{[] (std::stop_token stopToken) { while (!stopToken.stop_requested()) { /* */ }}});
threads.emplace_back(std::jthread{[] (std::stop_token stopToken) { while (!stopToken.stop_requested()) { /* */ }}});

C++20 std::jthread 允许采用 std::stop_token 的函数接收停止信号。析构函数 std::~jthread() 首先通过 token 请求停止,然后加入,因此在上述设置中基本上不需要手动清理。不幸的是,目前只有 MSVC STL 和 libstdc++ 支持它,而 Clang 的 libc++ 不支持。但如果您想进行一些练习,那么在 std::thread 之上实现自己就很容易了。

关于c++ - 如何在C++中加入多个不停止的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71501540/

相关文章:

c# - 如何并行运行两个线程?

multithreading - Haskell:TMVar 与 MVar

java - 关于并行运行线程并在之后返回

multithreading - 在 Mac OS X 上使用 clang 创建 std::thread 时出错: "attempt to use a deleted function"

c++ - 运行一个应用程序并以便携的方式忘记

c++ - SubsetSum 打印列表

c++ - 如何使用 windows.h 的函数更改 C++ 中的控制台字体大小?

c++ - 如何包装对 `std::thread` 构造函数的调用? (适用于 gcc、VS 和 icpc)

c++ - 良好做法:如何定义外部库的编译路径

c++ - 变量未在此范围内声明