RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
BS_thread_pool.hpp File Reference

BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library. This header file contains the entire library, and is the only file needed to use the library. More...

#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <future>
#include <iostream>
#include <limits>
#include <memory>
#include <mutex>
#include <optional>
#include <queue>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
Include dependency graph for BS_thread_pool.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  BS::version
 A struct used to store a version number, which can be checked and compared at compilation time. More...
class  BS::move_only_function< R(Args...)>
 A simple polyfill for std::move_only_function, to be used if C++23 features are not available. Note that it does not have all the features of std::move_only_function, only the minimum needed for the thread pool library. More...
struct  BS::move_only_function< R(Args...)>::func_concept
struct  BS::move_only_function< R(Args...)>::func_model< F >
struct  BS::pr_task
 A helper struct to store a task with an assigned priority. More...
class  BS::multi_future< T >
 A helper class to facilitate waiting for and/or getting the results of multiple futures at once. More...
class  BS::blocks< T >
 A helper class to divide a range into blocks. Used by detach_blocks(), submit_blocks(), detach_loop(), and submit_loop(). More...
struct  BS::block_task< T, F, R >
 A function object class used by detach_blocks() and submit_blocks() to execute a block function over a specified range of indices. More...
struct  BS::loop_task< T, F >
 A function object class used by detach_loop() and submit_loop() to execute a loop function over a specified range of indices. More...
struct  BS::sequence_task< T, F, R >
 A function object class used by detach_sequence() and submit_sequence() to execute a sequence function over a specified index. More...
struct  BS::task_and_future< R >
 A class that takes a function with a return value (but no arguments), and constructs a task with no return value along with a future used to retrieve the function's return value once the task is executed. Used by submit_task() and submit_bulk(). More...
class  BS::this_thread
 A class used to obtain information about the current thread and, if native extensions are enabled, get/set its priority, affinity, or name. More...
struct  BS::common_index_type< T1, T2, Enable >
 A meta-programming template to determine the common type of two integer types. Unlike std::common_type, this template maintains correct signedness. More...
struct  BS::common_index_type< T1, T2, std::enable_if_t< std::is_signed_v< T1 > &&std::is_signed_v< T2 > > >
struct  BS::common_index_type< T1, T2, std::enable_if_t< std::is_unsigned_v< T1 > &&std::is_unsigned_v< T2 > > >
struct  BS::common_index_type< T1, T2, std::enable_if_t<(std::is_signed_v< T1 > &&std::is_unsigned_v< T2 >)||(std::is_unsigned_v< T1 > &&std::is_signed_v< T2 >)> >
class  BS::thread_pool< OptFlags >
 A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. More...
class  BS::synced_stream
 A utility class to synchronize printing to one or more output streams by different threads. More...

Namespaces

namespace  BS
 A namespace used by Barak Shoshany's projects.

Macros

#define BS_THREAD_POOL_VERSION_MAJOR   5
#define BS_THREAD_POOL_VERSION_MINOR   1
#define BS_THREAD_POOL_VERSION_PATCH   0
#define BS_THREAD_POOL_DEFINE_BITWISE_OPERATOR(ENUM, OP)
#define BS_THREAD_POOL_WORKER_TOKEN
#define BS_THREAD_POOL_WAIT_TOKEN
#define BS_THREAD_POOL_STOP_CONDITION   !workers_running
#define BS_THREAD_POOL_OR_STOP_CONDITION   || !workers_running
#define BS_THREAD_POOL_IF_PAUSE_ENABLED   template <bool P = pause_enabled, typename = std::enable_if_t<P>>
#define BS_THREAD_POOL_INIT_FUNC_CONCEPT(F)

Typedefs

using BS::opt_t = std::uint8_t
 The type used for the bitmask template parameter of the thread pool.
using BS::task_t = move_only_function<void()>
 The type of tasks in the task queue.
using BS::thread_t = std::thread
 The type of threads to use. In C++17 we use std::thread.
using BS::priority_t = std::int8_t
 A type used to indicate the priority of a task. Defined to be a signed integer with a width of exactly 8 bits (-128 to +127).
template<typename T1, typename T2>
using BS::common_index_type_t = typename common_index_type<T1, T2>::type
 A helper type alias to obtain the common type from the template BS::common_index_type.
using BS::light_thread_pool = thread_pool<tp::none>
 A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with all optional features disabled.
using BS::priority_thread_pool = thread_pool<tp::priority>
 A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with task priority enabled.
using BS::pause_thread_pool = thread_pool<tp::pause>
 A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with pausing enabled.
using BS::wdc_thread_pool = thread_pool<tp::wait_deadlock_checks>
 A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with wait deadlock checks enabled.

Enumerations

enum class  BS::tp : opt_t { BS::none = 0 , BS::priority = 1 << 0 , BS::pause = 1 << 1 , BS::wait_deadlock_checks = 1 << 2 }
 An enumeration class of flags to be used in the bitmask template parameter of BS::thread_pool to enable optional features. More...
enum  BS::pr : priority_t {
  BS::lowest = -128 , BS::low = -64 , BS::normal = 0 , BS::high = +64 ,
  BS::highest = +127
}
 An enum containing some pre-defined priorities for convenience. More...

Functions

constexpr version BS::thread_pool_version (BS_THREAD_POOL_VERSION_MAJOR, BS_THREAD_POOL_VERSION_MINOR, BS_THREAD_POOL_VERSION_PATCH)
 The version of the thread pool library.
constexpr tp BS::operator~ (const tp value) noexcept

Variables

constexpr bool BS::thread_pool_module = false
 A flag indicating whether the thread pool library was compiled as a C++20 module.
constexpr bool BS::thread_pool_import_std = false
 A flag indicating whether the thread pool library imported the C++23 Standard Library module using import std.
constexpr bool BS::thread_pool_native_extensions = false
 A flag indicating whether the thread pool library's native extensions are enabled.

Detailed Description

BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library. This header file contains the entire library, and is the only file needed to use the library.

██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████

Author
Barak Shoshany (barak.nosp@m.sh@g.nosp@m.mail..nosp@m.com) (https://baraksh.com/)
Version
5.1.0
Date
2026-01-03

Macro Definition Documentation

◆ BS_THREAD_POOL_DEFINE_BITWISE_OPERATOR

#define BS_THREAD_POOL_DEFINE_BITWISE_OPERATOR ( ENUM,
OP )
Value:
constexpr ENUM operator OP(const ENUM lhs, const ENUM rhs) noexcept \
{ \
return static_cast<ENUM>(static_cast<std::underlying_type_t<ENUM>>(lhs) OP static_cast<std::underlying_type_t<ENUM>>(rhs)); \
} \
constexpr ENUM& operator OP##=(ENUM& lhs, const ENUM rhs) noexcept \
{ \
return lhs = lhs OP rhs; \
}
273#define BS_THREAD_POOL_DEFINE_BITWISE_OPERATOR(ENUM, OP) \
274 constexpr ENUM operator OP(const ENUM lhs, const ENUM rhs) noexcept \
275 { \
276 return static_cast<ENUM>(static_cast<std::underlying_type_t<ENUM>>(lhs) OP static_cast<std::underlying_type_t<ENUM>>(rhs)); \
277 } \
278 constexpr ENUM& operator OP##=(ENUM& lhs, const ENUM rhs) noexcept \
279 { \
280 return lhs = lhs OP rhs; \
281 }

◆ BS_THREAD_POOL_IF_PAUSE_ENABLED

#define BS_THREAD_POOL_IF_PAUSE_ENABLED   template <bool P = pause_enabled, typename = std::enable_if_t<P>>

◆ BS_THREAD_POOL_INIT_FUNC_CONCEPT

#define BS_THREAD_POOL_INIT_FUNC_CONCEPT ( F)
Value:
typename F, typename = std::enable_if_t<std::is_invocable_v<F> || std::is_invocable_v<F, std::size_t>>

◆ BS_THREAD_POOL_OR_STOP_CONDITION

#define BS_THREAD_POOL_OR_STOP_CONDITION   || !workers_running

◆ BS_THREAD_POOL_STOP_CONDITION

#define BS_THREAD_POOL_STOP_CONDITION   !workers_running

◆ BS_THREAD_POOL_VERSION_MAJOR

#define BS_THREAD_POOL_VERSION_MAJOR   5

◆ BS_THREAD_POOL_VERSION_MINOR

#define BS_THREAD_POOL_VERSION_MINOR   1

◆ BS_THREAD_POOL_VERSION_PATCH

#define BS_THREAD_POOL_VERSION_PATCH   0

◆ BS_THREAD_POOL_WAIT_TOKEN

#define BS_THREAD_POOL_WAIT_TOKEN

◆ BS_THREAD_POOL_WORKER_TOKEN

#define BS_THREAD_POOL_WORKER_TOKEN