命名空间
变体
操作

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)
危险指针
原子类型
(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),则所有注册了对与 jthread 内部停止状态关联的 std::stop_token 进行可中断等待的基本类型 std::condition_variable_any 的条件变量都将被唤醒。

[编辑] 示例

#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