命名空间
变体
操作

std::stop_callback

来自 cppreference.com
< cpp‎ | thread
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协作式取消
(C++20)
stop_callback
(C++20)
互斥
(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 中已弃用)
内存排序
原子操作的自由函数
原子标志的自由函数
 
 
定义在头文件 <stop_token>
template< class Callback >
class stop_callback;
(自 C++20 起)

stop_callback 类模板提供了一个 RAII 对象类型,它为关联的 std::stop_token 对象注册一个回调函数,以便当 std::stop_token 的关联 std::stop_source 被请求停止时,将调用回调函数。

通过 stop_callback 的构造函数注册的回调函数将在成功调用 request_stop() 的同一个线程中调用,该线程是 stop_callback 的关联 std::stop_tokenstd::stop_source;或者,如果在构造函数注册之前已经请求停止,则回调将在构造 stop_callback 的线程中调用。

可以为同一个 std::stop_token 创建多个 stop_callback,它们来自同一线程或不同的线程并发地创建。不保证它们执行的顺序,但它们将被同步调用;除了在为 std::stop_token 请求停止之后构建的 stop_callback,如前所述。

如果回调的调用通过异常退出,则将调用 std::terminate

std::stop_callback 不是 可复制构造可复制赋值可移动构造 或者 可移动赋值.

模板参数 Callback 类型必须同时是 可调用可析构。任何返回值都将被忽略。

内容

[编辑] 成员类型

类型 定义
callback_type Callback

[编辑] 成员函数

构造新的 stop_callback 对象
(公有成员函数) [编辑]
析构 stop_callback 对象
(公有成员函数) [编辑]
operator=
[已删除]
stop_callback 不可赋值
(公有成员函数) [编辑]

[编辑] 推断指南

[编辑] 示例

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
 
using namespace std::chrono_literals;
 
// Use a helper class for atomic std::cout streaming.
class Writer
{
    std::ostringstream buffer;
public:
    ~Writer()
    {
        std::cout << buffer.str();
    }
    Writer& operator<<(auto input)
    {
        buffer << input;
        return *this;
    }
};
 
int main()
{
    // A worker thread.
    // It will wait until it is requested to stop.
    std::jthread worker([] (std::stop_token stoken)
    {
        Writer() << "Worker thread's id: " << std::this_thread::get_id() << '\n';
        std::mutex mutex;
        std::unique_lock lock(mutex);
        std::condition_variable_any().wait(lock, stoken,
            [&stoken] { return stoken.stop_requested(); });
    });
 
    // Register a stop callback on the worker thread.
    std::stop_callback callback(worker.get_stop_token(), []
    {
        Writer() << "Stop callback executed by thread: "
            << std::this_thread::get_id() << '\n';
    });
 
    // Stop_callback objects can be destroyed prematurely to prevent execution.
    {
        std::stop_callback scoped_callback(worker.get_stop_token(), []
        {
            // This will not be executed.
            Writer() << "Scoped stop callback executed by thread: "
                << std::this_thread::get_id() << '\n';
        });
    }
 
    // Demonstrate which thread executes the stop_callback and when.
    // Define a stopper function.
    auto stopper_func = [&worker]
    {
        if (worker.request_stop())
            Writer() << "Stop request executed by thread: "
                << std::this_thread::get_id() << '\n';
        else
            Writer() << "Stop request not executed by thread: "
                << std::this_thread::get_id() << '\n';
    };
 
    // Let multiple threads compete for stopping the worker thread.
    std::jthread stopper1(stopper_func);
    std::jthread stopper2(stopper_func);
    stopper1.join();
    stopper2.join();
 
    // After a stop has already been requested,
    // a new stop_callback executes immediately.
    Writer() << "Main thread: " << std::this_thread::get_id() << '\n';
    std::stop_callback callback_after_stop(worker.get_stop_token(), []
    {
        Writer() << "Stop callback executed by thread: "
            << std::this_thread::get_id() << '\n';
    });
}

可能的输出

Worker thread's id: 140460265039616
Stop callback executed by thread: 140460256646912
Stop request executed by thread: 140460256646912
Stop request not executed by thread: 140460248254208
Main thread: 140460265043776
Stop callback executed by thread: 140460265043776