1 #if !defined(_INTERRUPTIBLE_HH_)
2 #define _INTERRUPTIBLE_HH_
9 #include <condition_variable>
17 #if HAVE_THIS_THREAD_SLEEP_FOR
18 template <
class Rep,
class Period>
19 void sleep_for(std::chrono::duration<Rep, Period> sleep_duration)
24 template <
class Rep,
class Period>
25 void sleep_for(std::chrono::duration<Rep, Period> sleep_duration)
27 std::condition_variable c;
29 std::unique_lock<std::mutex> l(m);
30 c.wait_for(l, sleep_duration);
36 template <
typename Rtype,
typename ... Args>
48 Rtype
SyncRun(std::chrono::milliseconds timeout, Args... args)
50 timeout_expired =
false;
51 std::future<Rtype> result = std::async(std::launch::async, this->
MakeFunction(), std::ref(args) ...);
54 if (timeout.count() != 0)
56 result.wait_for(timeout);
57 if (result.wait_for(std::chrono::milliseconds::zero()) != std::future_status::ready)
59 timeout_expired =
true;
74 std::shared_future<Rtype>
AsyncRun(std::chrono::milliseconds timeout, Args... args)
76 timeout_expired =
false;
77 std::shared_future<Rtype> result = std::async(std::launch::async, this->
MakeFunction(), std::ref(args) ...);
80 if (timeout.count() != 0)
82 std::thread t([
this, timeout, result]()
86 if (result.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) {
87 timeout_expired =
true;
99 timeout_expired =
true;
110 return [](Args& ...) -> Rtype {
121 std::atomic<bool> timeout_expired;
Rtype SyncRun(std::chrono::milliseconds timeout, Args...args)
virtual std::function< Rtype(Args &...)> MakeFunction()
std::shared_future< Rtype > AsyncRun(std::chrono::milliseconds timeout, Args...args)
void sleep_for(std::chrono::duration< Rep, Period > sleep_duration)
const std::atomic< bool > & TimeoutExpired()
virtual void AtTimeoutExpired()