Namespaces
Variants

std::jthread:: request_stop

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
bool request_stop ( ) noexcept ;
(自 C++20 起)

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

该判断以原子方式执行,如果已请求停止,停止状态会以原子方式更新以避免竞态条件,具体表现为:

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

然而,请参阅注释部分。

目录

参数

(无)

返回值

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

后置条件

对于通过 get_stop_token ( ) 获取的 std::stop_token 或通过 get_stop_source ( ) 获取的 std::stop_source stop_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;
// 用于快速显示哪个线程打印了什么的辅助函数
void print(auto txt)
{
    std::cout << std::this_thread::get_id() << ' ' << txt;
}
int main()
{
    // 一个睡眠的工作线程
    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");
            }
        });
    // 一个等待的工作线程
    // 条件变量将通过停止请求被唤醒
    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;
        });
    // 使此线程睡眠以给线程时间运行
    std::this_thread::sleep_for(400ms);
    // 可以显式调用 std::jthread::request_stop():
    print("Requesting stop of sleepy worker\n");
    sleepy_worker.request_stop();
    sleepy_worker.join();
    print("Sleepy worker joined\n");
    // 或者使用 RAII 自动调用:
    // waiting_worker 的析构函数将自动调用 request_stop()
    // 并自动加入线程
}

可能的输出:

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