summaryrefslogtreecommitdiff
path: root/src/discord/waiter.hpp
blob: 0d5ae928d0acd3b325023c28a8fbeb6c2c12ac50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once
#include <chrono>
#include <condition_variable>
#include <mutex>

class Waiter {
public:
    template<class R, class P>
    bool wait_for(std::chrono::duration<R, P> const &time) const {
        std::unique_lock<std::mutex> lock(m);
        return !cv.wait_for(lock, time, [&] { return terminate; });
    }

    void kill() {
        std::unique_lock<std::mutex> lock(m);
        terminate = true;
        cv.notify_all();
    }

    void revive() {
        std::unique_lock<std::mutex> lock(m);
        terminate = false;
    }

private:
    mutable std::condition_variable cv;
    mutable std::mutex m;
    bool terminate = false;
};