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>
56 void SetValue(
const T2 &value);
63 template <
typename ExceptionT>
64 void SetException(ExceptionT &&exception);
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")) {}
95 : _exception(
std::move(e)) {}
97 const char *
what() const noexcept
override {
98 return _exception->what();
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))));
std::map< const char *, mapped_type > _map
void SetValue(const T2 &value)
Set the value and notify all waiting threads.
void throw_exception(const std::exception &e)
void SetException(ExceptionT &&exception)
Set a exception, this exception will be thrown on all the threads waiting.
This file contains definitions of common data structures used in traffic manager. ...
static thread_local const char thread_tag
std::condition_variable _cv
const char * what() const noexcept override
This class is meant to be used similar to a shared future, but the value can be set any number of tim...
std::shared_ptr< std::exception > GetException() const
boost::variant2::variant< SharedException, T > value
Positive time duration up to milliseconds resolution.
boost::optional< T > WaitFor(time_duration timeout)
Wait until the next value is set.
constexpr auto to_chrono() const
SharedException(std::shared_ptr< std::exception > e)
std::shared_ptr< std::exception > _exception