You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+93-29Lines changed: 93 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,28 @@
1
1
# Dispatch Queue
2
-
Dispatch Queue / Thread Pool implementation for C++11.
2
+
Dispatch Queue / Thread Pool implementation for C++11 with built-in C++20 coroutine support.
3
3
4
4
5
5
## Features
6
-
- No external dependencies: uses only C++11 threading functionality
7
-
-`dispatch` method with API similar to [std::async](https://en.cppreference.com/w/cpp/thread/async): receives a functor and its arguments, returns a `std::future`
8
-
-`dispatch_forget` for a "fire and forget" style call, which avoids the overhead of creating the `std::future`
9
-
- Supports both synchronous (immediate) and asynchronous (threaded) execution.
10
-
+ Asynchronous dispatch queues are also known as Thread Pools
11
-
+ Synchronous dispatch queues are useful for multiplatform code that must work on platforms without thread support, for example WebAssembly on browsers that lack `SharedArrayBuffer` support
12
-
- Single implementation file [src/dispatch_queue.cpp](src/dispatch_queue.cpp), easy to integrate in any project
6
+
- No external dependencies: uses only the C++ STL
7
+
- Supports both immediate and threaded execution modes:
8
+
+ Threaded dispatch queues are also known as Thread Pools.
9
+
In threaded mode it is safe to dispatch new tasks from any thread.
10
+
+ In immediate mode tasks are executed immediately. Useful for multiplatform code that must work on platforms without thread support, for example WebAssembly on browsers that lack `SharedArrayBuffer` support.
11
+
- Use `dispatch_queue.dispatch(f, args...)` to dispatch new tasks
12
+
- Use `dispatch_queue.dispatch_main(f, args...)` to dispatch "main loop" tasks
13
+
+ Users must call `dispatch_queue.main_loop()` manually where appropriate to run queued main loop tasks
14
+
+ Useful for synchronizing state calculated in background tasks with the application's main loop
15
+
- Returned `dispatch_queue::task<T>` from dispatch methods are similar to `std::shared_future`, with the following additions:
16
+
+ Use `task.get_state()` to get whether task is pending, ready or failed with exception
17
+
+ Use `task.then(f)` to add a continuation function that runs when task finishes
18
+
+ Use `task.get_exception()` to get stored exception_ptr
19
+
- Built-in C++20 coroutine support
20
+
+ Use `dispatch_queue::task<T>` as the return value for your coroutines
21
+
+`co_await` other tasks to resume the coroutine as the task's continuation
22
+
+ Use `co_await dispatch_queue.dispatch()` to continue coroutine in a dispatch queue's background loop
23
+
+ Use `co_await dispatch_queue.dispatch_main()` to continue coroutine in a dispatch queue's main loop
24
+
- Supports compiling with `-fno-exceptions` and `-fno-rtti`
25
+
- Unified implementation file [src/dispatch_queue-one.cpp](src/dispatch_queue-one.cpp), easy to integrate in any project
13
26
14
27
15
28
## Usage example
@@ -20,14 +33,14 @@ Dispatch Queue / Thread Pool implementation for C++11.
0 commit comments