Dispatch Queue
Dispatch Queue / Thread Pool implementation for C++11 with built-in C++20 coroutine support
 
Loading...
Searching...
No Matches
dispatch_queue::dispatch_queue Class Reference

Public Member Functions

 dispatch_queue ()
 
 dispatch_queue (int thread_count)
 
template<typename Fn>
 dispatch_queue (int thread_count, Fn &&worker_init)
 
 dispatch_queue (const dispatch_queue &)=delete
 
dispatch_queueoperator= (const dispatch_queue &)=delete
 
 ~dispatch_queue ()
 
template<typename F, typename... Args, typename Ret = detail::function_result<F, Args...>>
task< Ret > dispatch (F &&f, Args &&... args)
 
template<typename F, typename... Args, typename Ret = detail::function_result<F, Args...>>
task< Ret > dispatch_main (F &&f, Args &&... args)
 
bool is_threaded () const
 
int thread_count () const
 
size_t size () const
 
bool empty () const
 
void clear ()
 
void main_loop ()
 
void wait ()
 
template<class Rep, class Period>
bool wait_for (const std::chrono::duration< Rep, Period > &timeout_duration)
 
template<class Clock, class Duration>
bool wait_until (const std::chrono::time_point< Clock, Duration > &timeout_time)
 
void shutdown ()
 
dispatch_awaiter dispatch ()
 
dispatch_main_awaiter dispatch_main ()
 

Constructor & Destructor Documentation

◆ dispatch_queue() [1/3]

dispatch_queue::dispatch_queue::dispatch_queue ( )

Create an immediate dispatch queue. In immediate mode, tasks are executed immediately when calling dispatch.

◆ dispatch_queue() [2/3]

dispatch_queue::dispatch_queue::dispatch_queue ( int thread_count)

Initializes dispatch queue with thread_count background threads and a no-op worker_init.

See also
dispatch_queue(int, Fn&&)

◆ dispatch_queue() [3/3]

template<typename Fn>
dispatch_queue::dispatch_queue::dispatch_queue ( int thread_count,
Fn && worker_init )
inline

Initializes dispatch queue with thread_count background threads and a worker initialization functor.

Parameters
thread_countNumber of background threads used to run tasks. If 0, the dispatch queue is created in immediate mode. If 1, tasks will run serially in background, one at a time, without any concurrency. Otherwise, thread_count threads will be created and tasks may run concurrently. Pass a negative number to use the default value of std::thread::hardware_concurrency() threads.
worker_initFunctor called inside worker threads for initialization, receiving as argument the worker index. May be used to set the thread name or initialize thread local variables, for example.

◆ ~dispatch_queue()

dispatch_queue::dispatch_queue::~dispatch_queue ( )

Calls shutdown.

Member Function Documentation

◆ clear()

void dispatch_queue::dispatch_queue::clear ( )

Cancel pending tasks, clearing the current queue. Tasks that are being processed will still run to completion.

◆ dispatch() [1/2]

dispatch_awaiter dispatch_queue::dispatch_queue::dispatch ( )
inline

Returns an awaiter that resumes a coroutine using dispatch when co_awaited.

co_await dispatch_queue.dispatch();
do_something_in_background();
}
dispatch_queue()
Definition dispatch_queue.cpp:5
Definition task.hpp:22

◆ dispatch() [2/2]

template<typename F, typename... Args, typename Ret = detail::function_result<F, Args...>>
task< Ret > dispatch_queue::dispatch_queue::dispatch ( F && f,
Args &&... args )
inline

Dispatch a task that calls f with forwarded arguments args. If the dispatch queue is in immediate mode, the task is processed immediately in the calling thread.

Parameters
fFunctor to be executed
argsArguments forwarded to f
Returns
Future for getting f result.

◆ dispatch_main() [1/2]

dispatch_main_awaiter dispatch_queue::dispatch_queue::dispatch_main ( )
inline

Returns an awaiter that resumes a coroutine using dispatch_main when co_awaited.

co_await dispatch_queue.dispatch_main();
do_something_in_main_loop();
}

◆ dispatch_main() [2/2]

template<typename F, typename... Args, typename Ret = detail::function_result<F, Args...>>
task< Ret > dispatch_queue::dispatch_queue::dispatch_main ( F && f,
Args &&... args )
inline

Dispatch a task that calls f with forwarded arguments args in main loop. Tasks dispatched with dispatch_main will only be executed when calling main_loop.

Parameters
fFunctor to be executed
argsArguments forwarded to f
Returns
Future for getting f result.
See also
main_loop

◆ empty()

bool dispatch_queue::dispatch_queue::empty ( ) const

Returns whether queue is empty, that is, there are no tasks queued.

◆ is_threaded()

bool dispatch_queue::dispatch_queue::is_threaded ( ) const

Whether this dispatch queue uses threads for processing tasks.

◆ main_loop()

void dispatch_queue::dispatch_queue::main_loop ( )

Invoke main loop tasks dispatched using dispatch_main. This should be called in your application's main loop.

◆ shutdown()

void dispatch_queue::dispatch_queue::shutdown ( )

Cancel pending tasks, wait and release the used threads. The queue now runs in immediate mode. It is safe to call this more than once.

◆ size()

size_t dispatch_queue::dispatch_queue::size ( ) const

Returns the number of queued tasks;

◆ thread_count()

int dispatch_queue::dispatch_queue::thread_count ( ) const

Number of threads used for processing tasks. This will be 0 in immediate mode.

◆ wait()

void dispatch_queue::dispatch_queue::wait ( )

Wait until all pending tasks finish processing.

◆ wait_for()

template<class Rep, class Period>
bool dispatch_queue::dispatch_queue::wait_for ( const std::chrono::duration< Rep, Period > & timeout_duration)
inline

Wait until all pending tasks finish processing. Blocks until specified timeout_duration has elapsed or all queued tasks complete, whichever comes first.

Returns
true if all tasks finished processing, otherwise false.

◆ wait_until()

template<class Clock, class Duration>
bool dispatch_queue::dispatch_queue::wait_until ( const std::chrono::time_point< Clock, Duration > & timeout_time)
inline

Wait until all pending tasks finish processing. Blocks until the specified timeout_time has been reached or all queued tasks complete, whichever comes first.

Returns
true if all tasks finished processing, otherwise false.

The documentation for this class was generated from the following files: