1#ifndef NEFORCE_CORE_ASYNC_VIRTUAL_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_VIRTUAL_THREAD_HPP__
12#ifdef NEFORCE_STANDARD_20
18NEFORCE_BEGIN_NAMESPACE__
128class virtual_thread_scheduler {
139 virtual_thread_scheduler() :
153 cv_.wait(
lock, [
this] {
return shutdown_ || !task_queue_.empty(); });
155 if (shutdown_ && task_queue_.empty()) {
159 if (!task_queue_.empty()) {
160 handle = task_queue_.front();
177 static virtual_thread_scheduler instance;
202 for (
size_t i = 0; i < num_threads; ++i) {
203 workers_.emplace_back([
this] { worker_loop(); });
219 for (
auto& worker: workers_) {
220 if (worker.joinable()) {
283 template <
typename Func>
304 template <
typename Func>
307 vt.task_ = virtual_thread::create_task(_NEFORCE
forward<Func>(func));
349NEFORCE_END_NAMESPACE__
~virtual_thread_scheduler()
析构函数
void start_workers(size_t num_threads)
启动工作线程
static virtual_thread_scheduler & get_instance()
获取调度器实例
void schedule(coroutine_handle<> handle)
调度协程任务
virtual_thread & operator=(virtual_thread &&other) noexcept=default
移动赋值运算符
virtual_thread(const virtual_thread &)=delete
禁止拷贝构造
static virtual_thread_awaiter yield()
让出执行权
static void shutdown()
关闭调度器
virtual_thread & operator=(const virtual_thread &)=delete
禁止拷贝赋值
static virtual_thread start(Func &&func)
启动虚拟线程
static void initialize(size_t num_threads)
初始化调度器
static virtual_thread_task sleep(const int64_t ms)
睡眠指定毫秒数
virtual_thread()=default
默认构造函数
virtual_thread(virtual_thread &&other) noexcept=default
移动构造函数
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
long long int64_t
64位有符号整数类型
std::coroutine_handle< Promise > coroutine_handle
协程句柄
duration< int64_t, milli > milliseconds
毫秒持续时间
exception_ptr NEFORCE_API current_exception() noexcept
获取当前异常
NEFORCE_CONSTEXPR14 T exchange(T &val, U &&new_val) noexcept(is_nothrow_move_constructible_v< T > &&is_nothrow_assignable_v< T &, U >)
将新值赋给对象并返回旧值
NEFORCE_ALWAYS_INLINE_INLINE thread::native_handle_type handle() noexcept
获取当前线程句柄
coroutine_handle handle_
协程句柄
void await_resume() const noexcept
恢复协程
void await_suspend(coroutine_handle<> handle)
挂起协程
bool await_ready() const noexcept
检查是否准备就绪
suspend_never initial_suspend()
初始挂起点
void unhandled_exception()
未处理异常处理
suspend_always final_suspend() noexcept
最终挂起点
virtual_thread_task get_return_object()
获取返回对象
exception_ptr exception_
异常存储
coroutine_handle< promise_type > handle_
协程句柄
virtual_thread_task & operator=(const virtual_thread_task &)=delete
禁止拷贝赋值
virtual_thread_task(const virtual_thread_task &)=delete
禁止拷贝构造
virtual_thread_task(coroutine_handle< promise_type > h)
构造函数
virtual_thread_task(virtual_thread_task &&other) noexcept
移动构造函数
virtual_thread_task & operator=(virtual_thread_task &&other) noexcept
移动赋值运算符
~virtual_thread_task()
析构函数