1#ifndef MANTICORE_MANTICORE_HPP
2#define MANTICORE_MANTICORE_HPP
5#include <condition_variable>
35 std::condition_variable cv;
39 std::string fallback_error;
40 std::string error_message;
42 enum class Status :
char { FREE, PRIMED, FINISHED };
44 std::function<void()> fun;
46 bool initialized =
false;
48 return ncomplete == nthreads;
61 fallback_error = std::move(e);
62 error_message.clear();
63 status = Status::FREE;
90 std::lock_guard lck(run_lock);
114 template<
class Function_>
123 std::unique_lock lk(run_lock);
124 cv.wait(lk, [&]{
return status == Status::FREE; });
127 status = Status::PRIMED;
135 cv.wait(lk, [&]{
return status == Status::FINISHED; });
139 auto errcopy = error_message;
140 error_message.clear();
143 status = Status::FREE;
147 if (!errcopy.empty()) {
148 throw std::runtime_error(errcopy);
159 std::unique_lock lk(run_lock);
161 cv.wait(lk, [&]{
return status == Status::PRIMED || done(); });
168 }
catch (std::exception& x) {
170 error_message = x.what();
173 error_message = fallback_error;
176 status = Status::FINISHED;
Execute arbitrary functions on the main thread.
Definition manticore.hpp:33
void listen()
Definition manticore.hpp:157
void run(Function_ f)
Definition manticore.hpp:115
void initialize(size_t n)
Definition manticore.hpp:72
void finish_thread(bool notify=true)
Definition manticore.hpp:83
void initialize(size_t n, std::string e)
Definition manticore.hpp:58
Defines manticore classes and functions.