12#include <boost/optional.hpp>
15#pragma warning(disable:4583)
16#pragma warning(disable:4582)
17#include <boost/variant2/variant.hpp>
20#include <boost/variant2/variant.hpp>
23#include <condition_variable>
32 class SharedException;
55 template <
typename T2>
63 template <
typename ExceptionT>
70 std::condition_variable
_cv;
74 boost::variant2::variant<SharedException, T>
value;
77 std::map<const char *, mapped_type>
_map;
92 :
_exception(
std::make_shared<
std::runtime_error>(
"uninitialized SharedException")) {}
97 const char *
what() const noexcept
override {
112 template <
typename T>
114 std::unique_lock<std::mutex> lock(_mutex);
116 r.should_wait =
true;
117 if (!_cv.wait_for(lock, timeout.
to_chrono(), [&]() { return !r.should_wait; })) {
120 if (r.value.index() == 0) {
123 return boost::variant2::get<T>(std::move(r.value));
126 template <
typename T>
127 template <
typename T2>
129 std::lock_guard<std::mutex> lock(_mutex);
130 for (
auto &pair : _map) {
131 pair.second.should_wait =
false;
132 pair.second.value = value;
137 template <
typename T>
138 template <
typename ExceptionT>
140 SetValue(
SharedException(std::make_shared<ExceptionT>(std::forward<ExceptionT>(e))));
This class is meant to be used similar to a shared future, but the value can be set any number of tim...
std::map< const char *, mapped_type > _map
void SetException(ExceptionT &&exception)
Set a exception, this exception will be thrown on all the threads waiting.
void SetValue(const T2 &value)
Set the value and notify all waiting threads.
boost::optional< T > WaitFor(time_duration timeout)
Wait until the next value is set.
std::condition_variable _cv
const char * what() const noexcept override
SharedException(std::shared_ptr< std::exception > e)
std::shared_ptr< std::exception > GetException() const
std::shared_ptr< std::exception > _exception
Positive time duration up to milliseconds resolution.
constexpr auto to_chrono() const
static thread_local const char thread_tag
This file contains definitions of common data structures used in traffic manager.
void throw_exception(const std::exception &e)
boost::variant2::variant< SharedException, T > value