Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software 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 the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
BS_thread_pool_utils.hpp
Go to the documentation of this file.
1#ifndef BS_THREAD_POOL_UTILS_HPP
2#define BS_THREAD_POOL_UTILS_HPP
13#include <chrono> // std::chrono
14#include <future> // std::promise, std::shared_future
15#include <initializer_list> // std::initializer_list
16#include <iostream> // std::cout
17#include <memory> // std::make_unique, std::unique_ptr
18#include <mutex> // std::mutex, std::scoped_lock
19#include <ostream> // std::endl, std::flush, std::ostream
20#include <utility> // std::forward
21
25namespace BS {
26// Macros indicating the version of the thread pool utilities library.
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
30
34class [[nodiscard]] signaller
35{
36public:
40 signaller() : promise(), future(promise.get_future()) {}
41
42 // The copy constructor and copy assignment operator are deleted. The signaller works using a promise, which cannot be copied.
43 signaller(const signaller&) = delete;
44 signaller& operator=(const signaller&) = delete;
45
46 // The move constructor and move assignment operator are defaulted.
47 signaller(signaller&&) = default;
48 signaller& operator=(signaller&&) = default;
49
53 void ready()
54 {
55 promise.set_value();
56 }
57
61 void wait()
62 {
63 future.wait();
64 }
65
66private:
70 std::promise<void> promise;
71
75 std::shared_future<void> future;
76}; // class signaller
77
81class [[nodiscard]] synced_stream
82{
83public:
89 explicit synced_stream(std::ostream& stream = std::cout) : out_stream(stream) {}
90
91 // The copy and move constructors and assignment operators are deleted. The synced stream uses a mutex, which cannot be copied or moved.
92 synced_stream(const synced_stream&) = delete;
93 synced_stream(synced_stream&&) = delete;
94 synced_stream& operator=(const synced_stream&) = delete;
96
103 template <typename... T>
104 void print(T&&... items)
105 {
106 const std::scoped_lock stream_lock(stream_mutex);
107 (out_stream << ... << std::forward<T>(items));
108 }
109
116 template <typename... T>
117 void println(T&&... items)
118 {
119 print(std::forward<T>(items)..., '\n');
120 }
121
125 inline static std::ostream& (&endl)(std::ostream&) = static_cast<std::ostream& (&)(std::ostream&)>(std::endl);
126
130 inline static std::ostream& (&flush)(std::ostream&) = static_cast<std::ostream& (&)(std::ostream&)>(std::flush);
131
132private:
136 std::ostream& out_stream;
137
141 mutable std::mutex stream_mutex = {};
142}; // class synced_stream
143
147class [[nodiscard]] timer
148{
149public:
153 timer() = default;
154
160 [[nodiscard]] std::chrono::milliseconds::rep current_ms() const
161 {
162 return (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time)).count();
163 }
164
168 void start()
169 {
170 start_time = std::chrono::steady_clock::now();
171 }
172
176 void stop()
177 {
178 elapsed_time = std::chrono::steady_clock::now() - start_time;
179 }
180
186 [[nodiscard]] std::chrono::milliseconds::rep ms() const
187 {
188 return (std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_time)).count();
189 }
190
191private:
195 std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::steady_clock::now();
196
200 std::chrono::duration<double> elapsed_time = std::chrono::duration<double>::zero();
201}; // class timer
202} // namespace BS
203#endif
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