Line data Source code
1 : // Copyright (c) 2016 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef PIVX_THREADINTERRUPT_H 6 : #define PIVX_THREADINTERRUPT_H 7 : 8 : #include <atomic> 9 : #include <chrono> 10 : #include <condition_variable> 11 : #include <mutex> 12 : 13 : /* 14 : A helper class for interruptible sleeps. Calling operator() will interrupt 15 : any current sleep, and after that point operator bool() will return true 16 : until reset. 17 : */ 18 2886 : class CThreadInterrupt 19 : { 20 : public: 21 : explicit operator bool() const; 22 : void operator()(); 23 : void reset(); 24 : bool sleep_for(std::chrono::milliseconds rel_time); 25 : bool sleep_for(std::chrono::seconds rel_time); 26 : bool sleep_for(std::chrono::minutes rel_time); 27 : 28 : private: 29 : std::condition_variable cond; 30 : std::mutex mut; 31 : std::atomic<bool> flag; 32 : }; 33 : 34 : #endif // PIVX_THREADINTERRUPT_H