命名空间
变体
操作

std::jthread::request_stop

来自 cppreference.cn
< cpp‎ | thread‎ | jthread
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协同取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闩锁和屏障
(C++20)
(C++20)
期物
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
Hazard 指针
原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(C++20 中已弃用)
(C++11)(C++20 中已弃用)
内存顺序
(C++11)(C++26 中已弃用)
原子操作的自由函数
原子标志的自由函数
 
 
bool request_stop() noexcept;
(自 C++20 起)

向内部停止状态发出停止请求,如果尚未请求停止。

该确定是原子性的,如果已请求停止,则原子性地更新停止状态以避免竞争条件,以便

  • stop_requested()stop_possible() 可以并发地在相同共享停止状态的其他 std::stop_tokenstd::stop_source 上调用。
  • request_stop() 可以从多个线程并发地在同一 jthread 对象或与同一停止状态关联的其他 std::stop_source 对象上调用,并且只有一个会实际执行停止请求。

但是,请参阅“注意”部分。

目录

[编辑] 参数

(无)

[编辑] 返回值

true 如果此调用发出了停止请求,否则为 false

[编辑] 后置条件

对于通过 get_stop_token() 检索的 std::stop_token 或通过 get_stop_source() 检索的 std::stop_sourcestop_requested()true

[编辑] 注意

如果 request_stop() 发出停止请求(即,返回 true),则为同一关联停止状态注册的任何 std::stop_callbacks 将在发出 request_stop() 的同一线程上同步调用。 如果回调的调用通过异常退出,则会调用 std::terminate

如果已发出停止请求,则此函数返回 false。 但是,不能保证刚刚(成功)为同一停止状态请求停止的另一个线程或 std::stop_source 对象未仍在调用 std::stop_callback 函数的过程中。

如果 request_stop() 发出停止请求(即,返回 true),则所有基类型为 std::condition_variable_any 的条件变量都将唤醒,这些条件变量已注册用于等待与 jthread 的内部停止状态关联的 std::stop_token 的可中断等待。

[编辑] 示例

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
 
using namespace std::chrono_literals;
 
// Helper function to quickly show which thread printed what
void print(auto txt)
{
    std::cout << std::this_thread::get_id() << ' ' << txt;
}
 
int main()
{
    // A sleepy worker thread
    std::jthread sleepy_worker(
        [](std::stop_token stoken)
        {
            for (int i = 10; i; --i)
            {
                std::this_thread::sleep_for(300ms);
                if (stoken.stop_requested())
                {
                    print("Sleepy worker is requested to stop\n");
                    return;
                }
                print("Sleepy worker goes back to sleep\n");
            }
        });
 
    // A waiting worker thread
    // The condition variable will be awoken by the stop request.
    std::jthread waiting_worker(
        [](std::stop_token stoken)
        {
            std::mutex mutex;
            std::unique_lock lock(mutex);
            std::condition_variable_any().wait(lock, stoken, []{ return false; });
            print("Waiting worker is requested to stop\n");
            return;
        });
 
    // Sleep this thread to give threads time to spin
    std::this_thread::sleep_for(400ms);
 
    // std::jthread::request_stop() can be called explicitly:
    print("Requesting stop of sleepy worker\n");
    sleepy_worker.request_stop();
    sleepy_worker.join();
    print("Sleepy worker joined\n");
 
    // Or automatically using RAII:
    // waiting_worker's destructor will call request_stop()
    // and join the thread automatically.
}

可能的输出

140287602706176 Sleepy worker goes back to sleep
140287623300928 Requesting stop of sleepy worker
140287602706176 Sleepy worker is requested to stop
140287623300928 Sleepy worker joined
140287594313472 Waiting worker is requested to stop