C++26未引入Executor标准类型,而是以sender/receiver契约、schedule/then等组合原语替代;并发改进聚焦std::jthread自动停止与stop_token泛化。C++26 的
Executors 提案(P0443R14 及后续修订)**不会落地为标准库的一部分**——它已被拆解、重构,并以更小粒度的组件形式分散进入 C++26,核心目标不是“统一异步模型”,而是**提供可组合、可定制的执行抽象基元,供更高层库(如 std::async、std::jthread、协程库)底层复用**。
executor 类型给用户你不会在 C++26 标准头文件里看到 std::executor 或 std::thread_pool_executor。提案最终放弃“一刀切”的 executor 接口设计,转而聚焦于:
std::execution::sender 和 std::execution::receiver:定义异步操作的生产者/消费者契约(用于 std::async 和协程 awaiter 互操作)std::execution::schedule():最简调度原语,返回一个 sender,
std::execution::then()、std::execution::upon_error() 等适配器:用于组合 sender 链,但不强制绑定到具体线程或队列co_await 或 std::async,只是它们底层可能共享同一套 sender/receiver 调度语义,而非各自实现一套线程分发逻辑。
std::jthread 增强与 std::stop_token 泛化相比遥不可及的 executor 模型,C++26 更实在的并发改进集中在协作式取消和线程生命周期管理:
std::jthread 现在支持构造时传入 std::stop_token,且其析构自动调用 request_stop() —— 不再需要手动 join() 或 detach()
std::stop_source 和 std::stop_token 可跨 std::thread、std::jthread、甚至协程作用域传递,取消信号真正可穿透异步栈
std::this_thread::get_stop_token() 成为标准函数,协程中可直接获取当前线程的停止句柄#include#include #include void worker(std::stop_token stoken) { while (!stoken.stop_requested()) { // 工作中... std::this_thread::sleep_for(10ms); } } int main() { std::jthread t{worker}; // 自动绑定 stop_source,析构时 request_stop() // 不需要显式 t.request_stop() 或 t.join() }
如果你真想用类 executor 的调度模型,现在就得依赖外部库:
libunifex 提供 inline_scheduler、thread_pool、single_thread_context 等具体调度器,API 接近 P0443 原始设计submit()、schedule()、let_value() 等操作符可直接组合,且与 C++23 协程兼容std::execution::sender 并不 ABI 兼容,C++26 的 sender 是简化版,两者不能混用std::stop_token,那 executor 提案对你当前项目基本没影响。