1#ifndef BS_THREAD_POOL_UTILS_HPP
2#define BS_THREAD_POOL_UTILS_HPP
15#include <initializer_list>
27#define BS_THREAD_POOL_UTILS_VERSION_MAJOR 4
28#define BS_THREAD_POOL_UTILS_VERSION_MINOR 1
29#define BS_THREAD_POOL_UTILS_VERSION_PATCH 0
40 signaller() : promise(), future(promise.get_future()) {}
89 explicit synced_stream(std::ostream& stream = std::cout) : out_stream(stream) {}
103 template <
typename... T>
106 const std::scoped_lock stream_lock(stream_mutex);
107 (out_stream << ... << std::forward<T>(items));
116 template <
typename... T>
119 print(std::forward<T>(items)...,
'\n');
125 inline static std::ostream& (&
endl)(std::ostream&) =
static_cast<std::ostream& (&)(std::ostream&)
>(std::endl);
130 inline static std::ostream& (&
flush)(std::ostream&) =
static_cast<std::ostream& (&)(std::ostream&)
>(std::flush);
141 mutable std::mutex stream_mutex = {};
160 [[nodiscard]] std::chrono::milliseconds::rep
current_ms()
const
162 return (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time)).count();
170 start_time = std::chrono::steady_clock::now();
178 elapsed_time = std::chrono::steady_clock::now() - start_time;
186 [[nodiscard]] std::chrono::milliseconds::rep
ms()
const
188 return (std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_time)).count();
195 std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::steady_clock::now();
200 std::chrono::duration<double> elapsed_time = std::chrono::duration<double>::zero();
A utility class to allow simple signalling between threads.
Definition BS_thread_pool_utils.hpp:35
signaller()
Construct a new signaller.
Definition BS_thread_pool_utils.hpp:40
std::shared_future< void > future
A future used to wait for the signaller.
Definition BS_thread_pool_utils.hpp:75
std::promise< void > promise
A promise used to set the state of the signaller.
Definition BS_thread_pool_utils.hpp:70
void ready()
Inform any waiting threads that the signaller is ready.
Definition BS_thread_pool_utils.hpp:53
void wait()
Wait until the signaller is ready.
Definition BS_thread_pool_utils.hpp:61
A utility class to synchronize printing to an output stream by different threads.
Definition BS_thread_pool_utils.hpp:82
static std::ostream &(&) endl(std::ostream &)
A stream manipulator to pass to a synced_stream (an explicit cast of std::endl). Prints a newline cha...
Definition BS_thread_pool_utils.hpp:125
static std::ostream &(&) flush(std::ostream &)
A stream manipulator to pass to a synced_stream (an explicit cast of std::flush). Used to flush the s...
Definition BS_thread_pool_utils.hpp:130
void println(T &&... items)
Print any number of items into the output stream, followed by a newline character....
Definition BS_thread_pool_utils.hpp:117
void print(T &&... items)
Print any number of items into the output stream. Ensures that no other threads print to this stream ...
Definition BS_thread_pool_utils.hpp:104
synced_stream(std::ostream &stream=std::cout)
Construct a new synced stream.
Definition BS_thread_pool_utils.hpp:89
std::ostream & out_stream
The output stream to print to.
Definition BS_thread_pool_utils.hpp:136
A utility class to measure execution time for benchmarking purposes.
Definition BS_thread_pool_utils.hpp:148
std::chrono::milliseconds::rep ms() const
Get the number of milliseconds stored when stop() was last called.
Definition BS_thread_pool_utils.hpp:186
timer()=default
Construct a new timer and immediately start measuring time.
void start()
Start (or restart) measuring time. Note that the timer starts ticking as soon as the object is create...
Definition BS_thread_pool_utils.hpp:168
std::chrono::milliseconds::rep current_ms() const
Get the number of milliseconds that have elapsed since the object was constructed or since start() wa...
Definition BS_thread_pool_utils.hpp:160
void stop()
Stop measuring time and store the elapsed time since the object was constructed or since start() was ...
Definition BS_thread_pool_utils.hpp:176
softfloat & operator=(const softfloat &c)
A namespace used by Barak Shoshany's projects.
Definition BS_thread_pool.hpp:44