VLink  2.0.0
A high-performance communication middleware
vlink::CancellationToken类 参考final

Lightweight observer of a CancellationSource shared across worker threads. 更多...

#include <cancellation.h>

vlink::CancellationToken 的协作图:

Public 成员函数

 CancellationToken () noexcept
 Constructs an invalid token not bound to any source. 更多...
 
bool valid () const noexcept
 Reports whether the token observes a live cancellation source. 更多...
 
bool is_cancellation_requested () const noexcept
 Returns true once the bound source has been cancelled. 更多...
 
CancellationRegistration register_callback (MoveFunction< void()> &&callback) const
 Subscribes a one-shot callback to fire when the source is cancelled. 更多...
 
void throw_if_cancellation_requested () const
 Throws vlink::Exception::OperationCancelled when cancellation has been requested. 更多...
 

友元

class CancellationSource
 

详细描述

Lightweight observer of a CancellationSource shared across worker threads.

Tokens are cheap to copy and safe to hand to any thread; many tokens may observe the same source concurrently. A default-constructed token is invalid – it never reports cancellation and never accepts callbacks. Workers typically poll is_cancellation_requested at safe points, call throw_if_cancellation_requested at structured points, or install a one-shot callback via register_callback.

构造及析构函数说明

◆ CancellationToken()

vlink::CancellationToken::CancellationToken ( )
noexcept

Constructs an invalid token not bound to any source.

成员函数说明

◆ is_cancellation_requested()

bool vlink::CancellationToken::is_cancellation_requested ( ) const
noexcept

Returns true once the bound source has been cancelled.

返回
true after the source's request_cancel has succeeded.

◆ register_callback()

CancellationRegistration vlink::CancellationToken::register_callback ( MoveFunction< void()> &&  callback) const

Subscribes a one-shot callback to fire when the source is cancelled.

When cancellation has not yet been requested the callback is stored in the source and fires once on the thread that later calls CancellationSource::request_cancel. When cancellation already happened the callback runs synchronously inside this call and the returned registration is empty. Exceptions escaping the callback are caught and logged via CLOG_E; they never propagate to the registering or cancelling thread.

参数
callbackMove-only nullary functor; an empty callback is silently ignored.
返回
RAII registration for the subscription, or an empty handle when cancellation had already fired or the token / callback was invalid.

◆ throw_if_cancellation_requested()

void vlink::CancellationToken::throw_if_cancellation_requested ( ) const

Throws vlink::Exception::OperationCancelled when cancellation has been requested.

异常
vlink::Exception::OperationCancelledif is_cancellation_requested() is true.

◆ valid()

bool vlink::CancellationToken::valid ( ) const
noexcept

Reports whether the token observes a live cancellation source.

返回
true when the token was minted from a CancellationSource.

友元及相关函数文档

◆ CancellationSource

friend class CancellationSource
friend

该类的文档由以下文件生成: