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

#include <dispatch_queue.hpp>

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)
 
template<typename F, typename... Args, typename Ret = detail::function_result<F, Args...>>
task< Ret > dispatch_tagged (task_tag tag, F &&f, Args &&... args)
 
template<typename F, typename It>
task< void > parallel_for (F &&f, const It &begin, const It &end, size_t batch_size=DISPATCH_QUEUE_DEFAULT_BATCH_SIZE)
 
template<typename F, typename R>
task< void > parallel_for (F &&f, R &&range, size_t batch_size=DISPATCH_QUEUE_DEFAULT_BATCH_SIZE)
 
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 ()
 
dispatch_tagged_awaiter dispatch_tagged (task_tag tag)
 

Constructor & Destructor Documentation

◆ dispatch_queue() [1/4]

dispatch_queue::dispatch_queue::dispatch_queue ( )

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

◆ dispatch_queue() [2/4]

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/4]

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() [4/4]

dispatch_queue::dispatch_queue::dispatch_queue ( const dispatch_queue & )
delete

◆ ~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();
}
Definition task.hpp:32
Definition dispatch_queue.hpp:19

◆ 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

◆ dispatch_tagged() [1/2]

dispatch_tagged_awaiter dispatch_queue::dispatch_queue::dispatch_tagged ( task_tag tag)
inline

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

co_await dispatch_queue.dispatch_tagged(tag);
do_something_with_tag();
}

◆ dispatch_tagged() [2/2]

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

Dispatch a tagged task that calls f with forwarded arguments args. Tasks tagged with the same value never run in parallel: at most one task is processed for each tag at a time. Use this to serialize different task types without having to create separate dispatch queues. If the dispatch queue is in immediate mode, the task is processed immediately in the calling thread.

Parameters
tagThe tag associated to the task
fFunctor to be executed
argsArguments forwarded to f
Returns
Future for getting f result.

◆ 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.

◆ operator=()

dispatch_queue & dispatch_queue::dispatch_queue::operator= ( const dispatch_queue & )
delete

◆ parallel_for() [1/2]

template<typename F, typename It>
task< void > dispatch_queue::dispatch_queue::parallel_for ( F && f,
const It & begin,
const It & end,
size_t batch_size = DISPATCH_QUEUE_DEFAULT_BATCH_SIZE )
inline

Iterate from begin until end applying f to each element in one or more dispatched tasks.

The range is chunked in batches of size batch_size, so each dispatched task is applied to at most batch_size elements. The returned task finishes when all batches finish.

◆ parallel_for() [2/2]

template<typename F, typename R>
task< void > dispatch_queue::dispatch_queue::parallel_for ( F && f,
R && range,
size_t batch_size = DISPATCH_QUEUE_DEFAULT_BATCH_SIZE )
inline

Iterate over range applying f to each element in one or more dispatched tasks.

The range is chunked in batches of size batch_size, so each dispatched task is applied to at most batch_size elements. The returned task finishes when all batches finish.

◆ 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: