Venus is yet another executor library, why? because I needed something simple and I couldn't find anything online.
The objective is to solve these common problems, one or more of these can apply:
- you have a data set that needs to be modified by multiple actors, possibly on different threads
- you need to parallelize work
- result(s) need to be synchronized/sequentialized periodically
- you need to dispatch work into the background and have a callback with the results
The main tools we'll be using are:
- executors (more details below)
- queues
- std::mutex
- std::threads
- std::condition_variable
If this sounds too trivial, that is good, because that's what I'm aiming for.
- Queues, Mutexes and Threads
- These are available from C++14 and we will use them as such, there are no dependencies other than the standard library.
- venus is header-only: copying
src/executor/includeinto your project is enough, CMake is optional.venus.hppis a single header with all of venus, it is generated by the build from the separate headers. - Tested with GCC 14.2.0 and MSVC 19.44 (Visual Studio 2022). CI builds and tests with GCC, Clang, AppleClang and MSVC, in C++14 and C++17, with address, undefined behavior and thread sanitizers.
- The examples are compiled and run with GCC 4.9.0 to 16.2 and Clang 3.5 to 15 on Compiler Explorer, see Testing with old compilers.
- To use the CMake files, cmake 3.18 or newer is required.
Each example opens in Compiler Explorer with GCC 4.9.2 and GCC 16.2, showing the assembly and the output of running it for both. It includes the single header from GitHub, so it always uses the latest version:
#include <https://raw.githubusercontent.com/janwilmans/venus/refs/heads/main/src/executor/include/venus.hpp>- venus::executor: many producers, one thread executes all tasks in order, timers (source)
- venus::pool_executor: parallel sum on all cores (source)
- venus::dispatcher: parallel work at low priority, results applied on a serial queue without locks (source)
python3 tools/compiler_explorer.py links creates new links, when the examples change.
With FetchContent:
include(FetchContent)
FetchContent_Declare(venus
GIT_REPOSITORY https://github.com/janwilmans/venus.git
GIT_TAG main # better: a specific commit
)
FetchContent_MakeAvailable(venus)
target_link_libraries(my_target PRIVATE venus::executor)Or, with venus in a subdirectory of your project, add_subdirectory(venus).
Or, installed with cmake --install, find_package(venus REQUIRED).
When venus is used by another project, its tests are not built (so googletest is not fetched), and it is not compiled with extra warnings or -Werror.
The headers are included as #include "executor/dispatcher.hpp", or all at once as #include "venus.hpp" (also when installed, they are installed in <prefix>/include/venus/).
| CMake option | Default | Meaning |
|---|---|---|
VENUS_BUILD_TESTS |
ON only as top-level build | build the tests and examples, fetches googletest |
VENUS_WARNINGS |
ON only as top-level build | extra warnings, and warnings as errors (unless CI_SERVER is set) |
VENUS_INSTALL |
ON only as top-level build | generate the install target, for find_package(venus) |
VENUS_SINGLE_HEADER |
ON only as top-level build | regenerate src/executor/include/venus.hpp during the build |
VENUS_SANITIZE |
empty | sanitizers to enable, for example address,undefined or thread |
Executors are not part of the C++ standard library, so we will implement them. Venus' Executors are not exactly the same idea as the 'executor proposal' or the 'parallel STL', I still used the name because I think it fits well. There are three kinds of executors available here:
A pool executor has one queue of work that needs to be processed. It gives no guarantees on when it will be processed, or in what order. You give it a number of threads to use (by default, one per core) and tasks will be picked up on a best effort basis. As soon as a working thread is done, and there is work in the queue, it is picked up and processed.
A Pool executor supports Parallelism, not to be confused with Concurrency. In Parallelism, tasks are by definition independent and do not access the same data. This is why it provides no 'thread safety' features whatsoever.
The goal of this executor is to utilize the multitude of cores in the CPU, you have tasks that need doing, let's get them done as soon as possible.
The Pool executor is very good to parallelize work, scatter work, if you like.
venus::pool_executor pool;
pool.add([] { /* independent work */ });
auto result = pool.call_async([] { return 42; }); // std::future<int>
pool.wait_idle();A single thread executor has one queue of work that needs to be processed. It gives a hard first-come-first-serve guarantee. As soon as work is queued, tasks are picked up and processed sequentially.
Single thread executor allows you to schedule work that is all done sequentially, this means that all work done by the executor is inherently thread safe without any significant contention on locks.
There is still a lock in the queueing mechanism, but instead of having locks that protect your data structure, likely multiple locks in different places that may have to be locked/unlocked several times, there is now only one lock that is only touched when work is queued.
The Single thread executor is used to synchronize work, gather tasks, so to say.
It also runs timers: call_at, call_after and call_every. Queued tasks always go before timers, so a continuous flood of tasks delays timers.
The dispatcher combines both ideas, inspired by Grand Central Dispatch, but without depending on it. It takes the parts that give most of the benefit:
- one fixed pool of threads, one per core by default, shared by all producers
- three priorities:
high,normalandlow; a thread always picks the oldest task of the highest priority dispatch()runs tasks in parallel, like the pool executormake_serial_queue()creates serial queues: tasks of one serial queue run one at a time, in order, like on a single thread executor. A serial queue is not a thread, it is a cheap object, so you can have one per producer, data structure or subsystem, and they all share the cores.
A serial queue with work occupies at most one thread at a time, and after a batch of 16 tasks it goes to the back of its priority, so one busy serial queue does not starve the others. Higher priority work does always go first.
venus::dispatcher dispatcher;
auto model = dispatcher.make_serial_queue(venus::priority::high);
dispatcher.dispatch([&] {
auto result = heavy_computation(); // runs in parallel
model.dispatch([&, result] { apply_to_model(result); }); // runs serialized, no locks needed
}, venus::priority::low);The dispatcher must outlive its serial queues. Tasks that were dispatched to a serial queue still run when the serial queue object is destroyed.
- Exceptions thrown by tasks started with
call(),call_async()anddispatch_async()are delivered through the returned future. Exceptions thrown by other tasks are caught and ignored, they do not stop the executor. - The destructors of
pool_executoranddispatcherwait until all tasks, including tasks added by tasks, are finished. The destructor ofexecutorruns the tasks that were queued before it, and discards timers that have not run yet. - A task that waits for its own executor would deadlock:
executor::synchronize()on the executor thread, andwait_idle()from a task on the same pool or dispatcher, throwstd::logic_errorinstead. - Using an executor while it is being destroyed violates the C++ object lifetime rules. Calling
cancel()on ascheduled_callafter its executor is destroyed does nothing.
Timers are based on std::chrono::steady_clock and should not be affected by adjustments of the system clock. However, libstdc++ before GCC 10 waits on system_clock internally, so on those compilers adjusting the system clock can make a timer fire early or late.
GCC 4.9 is the oldest supported compiler. Old compilers are rarely installed, so tools/compiler_explorer.py uses the Compiler Explorer API to compile and run the examples with them, the same way as the links above: with the single header (only the Python standard library is needed):
python3 tools/compiler_explorer.py test # GCC 4.9.0 to 16.2, Clang 3.5 to 15
python3 tools/compiler_explorer.py test g492 g540 # specific compilers, by Compiler Explorer id
python3 tools/compiler_explorer.py test --github # with venus.hpp from GitHub main, as the links use itThe test fails when a compiler reports a warning in the venus headers, because those warnings would show up in the builds of everyone using venus. CI runs this test as well.
The executors are intended to work together. If you have parallel work and you need to access data that other tasks can also access you might be tempted to add a synchronization primitive like a Mutex. However, if the task you queue on a Pool executor can block, you risk blocking other tasks and 'creating an idle core' while other work could be done.
Instead, use this strategy:
- first schedule a task on a Single thread executor (or serial queue) and either copy the data to work on or just do the work if it's a small task.
- at the end of that task, schedule the parallel work on a Pool executor (or dispatcher)
- at the end of the parallel work, schedule a task back on the Single thread executor (or serial queue) to synchronously process the result.
Guidelines:
- tasks on the Pool executor should not take locks or do blocking I/O (nor should they need to)
- it's OK for tasks on the Pool executor to take a long time
- tasks on the Single thread executor block all other tasks in its queue, so keep these tasks as short as possible.
References:
