|
ThreadSchedule 3.0.0
Modern C++ thread management library
|
ThreadSchedule is a C++17 library for creating, configuring, scheduling, and observing threads on Linux and Windows. It is header-only by default. C++20 consumers additionally get threadschedule::jthread when the standard library provides std::jthread.
The v3 core deliberately stays small and uses lowercase, standard-style names. Operations whose normal failure mode should not require exceptions return threadschedule::expected<T, std::error_code>.
The tested compiler versions are the compatibility contract. See Compatibility for the current matrix.
GCC 14's ThreadSanitizer can incorrectly report unlock of an unlocked mutex (or by a wrong thread) when a pool uses shutdown_for(...). libstdc++ acquires the timed mutex through pthread_mutex_clocklock, which GCC 14's TSan does not fully intercept, but it does observe the later unlock. This is a sanitizer false positive rather than an unmatched unlock in ThreadSchedule. The sanitizer CI therefore uses GCC 16, where the same tests pass cleanly.
The recommended source integration uses CMake FetchContent:
An existing checkout can be added directly:
To install and consume the CMake package:
Conan 2 consumers can build a local package directly from the release source:
Windows Vista compatibility mode is available when older platform targeting is required. It reduces Windows feature usage to avoid Win7+ only paths. This mode is currently not tested on real Vista hardware and may be unstable. Validation is limited because no active Vista test machine is available.
The recipe is tested in CI. Its standard shared=True option packages the optional ThreadSchedule::Runtime; header-only mode remains the default.
The complete getting-started project includes its own CMakeLists.txt and is tested against a freshly installed package.
| Need | Start with |
|---|---|
| Own one thread | thread |
| Own one cooperatively cancellable C++20 thread | jthread |
| Configure the calling thread | this_thread |
| Submit general-purpose work | thread_pool |
| Run delayed or periodic work | scheduled_pool |
| Discover and control registered threads | thread_registry |
| Find unregistered Linux threads by OS name | advanced::thread_by_name_view |
| Select a specialized pool or native control | advanced::* |
Include <threadschedule/threadschedule.hpp> for the complete core. Include <threadschedule/advanced.hpp> only when the workload requires native or specialized choices.
For small consumers, each core contract is independently includable. For example, a single managed thread needs only:
Pools can use <threadschedule/thread_pool.hpp> or <threadschedule/scheduled_pool.hpp> directly; registry-only code can use <threadschedule/thread_registry.hpp>. The focused headers avoid making an application opt into unrelated APIs, while threadschedule.hpp remains the convenient complete core umbrella.
ThreadSchedule keeps failure channels explicit:
| Operation | Failure channel |
|---|---|
| Direct construction | May throw std::system_error, like standard types |
create(...) | Returns expected<T, std::error_code> |
| Configuration and shutdown | Return expected<void, std::error_code> |
thread_pool::submit(...) | Submission error in expected; task exception in the future |
thread_pool::post(...) | Submission error in expected; task exception via the configured error callback |
Explicit *_or_throw operation | Throws std::system_error on failure |
Always inspect an expected before dereferencing it. A task submitted with post() has no future; call set_error_callback(...) on the pool config if its exceptions must be observed.
threadschedule::thread owns a std::thread but deliberately joins a joinable thread on destruction. Destruction and move assignment can therefore block. Call join(), detach(), or release() explicitly when that timing matters.
Direct construction is the ordinary path:
Use create(...) when initial configuration failures should be returned as an error value:
Affinity uses logical CPU indices and is intentionally absent from this first configured example: containers and restricted CPU sets may not make CPU 0 available. Query the deployment environment before pinning a thread.
Code running inside any thread can configure itself without wrapping or registering the thread first:
this_thread also provides configure, set_nice, get_priority, set_name, and get_name. Affinity readback reports the logical CPU indices the process is actually allowed to use, which is safer than assuming CPU 0 is available.
Under C++20, jthread mirrors standard callable forwarding and stop-token injection:
See the compile-tested jthread example.
Task exceptions from submit() remain attached to the returned future and are rethrown by get(). Direct pool construction can throw when worker creation or configuration fails; thread_pool::create(...) offers the error-value path.
Portable intent factories cover ordinary use:
The five priority_level values are the simplest cross-platform choice. Negative nice values and realtime policies normally require elevated privileges on Linux. Native scheduling remains available through threadschedule::advanced.
On Linux, an unregistered process thread can also be found by its exact kernel-visible name. A singular lookup rejects duplicate names; use find_all() when duplicates are intentional:
The view remembers the Linux TID and its start-time generation, so exited or recycled targets report no_such_process. This native lookup cannot fully close the race between the last identity check and a TID-based syscall; use thread_registry when target lifetime must be coupled to control operations.
The advanced namespace is public and follows semantic versioning. See Advanced APIs for native controls, profiles, topology, future combinators, task groups, chaos testing, and lower-level error handling.
Header-only mode owns one registry per linked image. Applications that need one registry shared by an executable and compatible DSOs can link the optional C++ runtime:
This is a same-toolchain C++ ABI, not a portable plugin ABI. Do not mix GCC, MinGW, and MSVC artifacts.
ThreadSchedule is available under the [MIT License](LICENSE).