30 lines
843 B
C++
30 lines
843 B
C++
|
|
#include "mix_cc/exception/exception_mt.h"
|
||
|
|
#include "mix_cc/algorithm/is_found.h"
|
||
|
|
#include <mutex>
|
||
|
|
#include <queue>
|
||
|
|
#include <unordered_map>
|
||
|
|
|
||
|
|
namespace mix_cc {
|
||
|
|
|
||
|
|
namespace exception_mt {
|
||
|
|
static std::mutex e_mutex;
|
||
|
|
static std::unordered_map<std::thread::id, std::exception> e_map;
|
||
|
|
} // namespace exception_mt
|
||
|
|
|
||
|
|
void storage_exception_mt(std::exception e, std::thread::id tid) {
|
||
|
|
std::lock_guard lg(exception_mt::e_mutex);
|
||
|
|
exception_mt::e_map.insert(std::make_pair(tid, e));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::optional<std::exception> receive_exception_mt(std::thread::id tid) {
|
||
|
|
std::lock_guard lg(exception_mt::e_mutex);
|
||
|
|
auto iter = exception_mt::e_map.find(tid);
|
||
|
|
if (is_found(exception_mt::e_map, iter)) {
|
||
|
|
auto ret = iter->second;
|
||
|
|
exception_mt::e_map.erase(iter);
|
||
|
|
return std::make_optional(ret);
|
||
|
|
}
|
||
|
|
return std::nullopt;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace mix_cc
|