44 worker_pool = std::make_unique<detail::worker_pool>(task_queue,
thread_count, worker_init);
63 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
65 return dispatch_internal(
false, std::forward<F>(f), std::forward<Args>(args)...);
76 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
78 return dispatch_internal(
true, std::forward<F>(f), std::forward<Args>(args)...);
84 bool is_threaded()
const;
90 int thread_count()
const;
124 template<
class Rep,
class Period>
125 bool wait_for(
const std::chrono::duration<Rep, Period>& timeout_duration) {
127 return worker_pool->wait_for(timeout_duration);
139 template<
class Clock,
class Duration>
140 bool wait_until(
const std::chrono::time_point<Clock, Duration>& timeout_time) {
142 return worker_pool->wait_until(timeout_time);
156#ifdef __cpp_lib_coroutine
158 struct dispatch_awaiter {
161 bool await_ready() const noexcept {
return false; }
162 void await_suspend(std::coroutine_handle<> cont)
const {
163 dispatch_queue.dispatch([cont]{
170 void await_resume() {}
173 struct dispatch_main_awaiter {
176 bool await_ready() const noexcept {
return false; }
177 void await_suspend(std::coroutine_handle<> cont)
const {
185 void await_resume() {}
199 return dispatch_awaiter(*
this);
212 return dispatch_main_awaiter(*
this);
217 std::unique_ptr<detail::worker_pool> worker_pool;
220 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
221 task<Ret> dispatch_internal(
bool run_on_main_loop, F&& f, Args&&... args) {
222 auto work = std::bind(std::move(f), std::forward<Args>(args)...);
224 auto future = detail::task_future<Ret>::create_pending();
225 worker_pool->enqueue_task({ future->wrap(work) }, run_on_main_loop);
226 return task<Ret>(future);
228 else if (run_on_main_loop) {
229 auto future = detail::task_future<Ret>::create_pending();
230 task_queue.push({ future->wrap(work) }, run_on_main_loop);
234 auto future = detail::task_future<Ret>::create(work);