stoppable_token — std::stoppable_token 개념
stoppable_token — std::stoppable_token 개념
std::stoppable_token은 **중지 가능한 토큰 타입이 갖춰야 하는 인터페이스를 설명하는 개념(concept)**이에요. C++26에서 도입됐어요. <stop_token> 헤더에 있어요.
std::stop_token 뿐 아니라 커스텀 중지 토큰 타입도 이 개념을 만족할 수 있어요.
출처: cppreference
본문
// <stop_token> 헤더, C++26
template< class Token >
concept stoppable_token =
requires (const Token tok) {
typename /*check-type-alias-exists*/<Token::template callback_type>;
{ tok.stop_requested() } noexcept -> std::same_as<bool>;
{ tok.stop_possible() } noexcept -> std::same_as<bool>;
// ... 등
};
stoppable_token<Token>은 다음을 요구해요.
Token::template callback_type<CallbackFn>타입 별칭 존재tok.stop_requested()—noexcept,bool반환tok.stop_possible()—noexcept,bool반환- 기타
stop_callback_for_t관련 요구
사용 예
#include <stop_token>
#include <concepts>
// 중지 가능한 토큰을 받는 제네릭 함수
template<std::stoppable_token Token>
void run_until_stopped(Token tok) {
while (!tok.stop_requested()) {
// 작업 수행
}
}
std::stop_token st;
std::jthread t([st] { run_until_stopped(st); });
특징
std::stop_token은 이 개념을 만족해요.- C++26에서 제네릭 중지 토큰 처리를 인터페이스로 추상화해요.
- 파생 개념으로
unstoppable_token이 있어요 (중지 불가능한 토큰).
static_assert(std::stoppable_token<std::stop_token>); // true
std::stoppable_token 개념은 중지 토큰 타입의 공통 인터페이스를 정의해서, 제네릭 코드가 어떤 중지 토큰이든 다룰 수 있게 해줘요. C++26 신규 기능이에요.