Bitcoin Core  21.99.0
P2P Digital Currency
threadinterrupt.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2019 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 BITCOIN_THREADINTERRUPT_H
6 #define BITCOIN_THREADINTERRUPT_H
7 
8 #include <sync.h>
9 
10 #include <atomic>
11 #include <chrono>
12 #include <condition_variable>
13 
14 /*
15  A helper class for interruptible sleeps. Calling operator() will interrupt
16  any current sleep, and after that point operator bool() will return true
17  until reset.
18 */
20 {
21 public:
23  explicit operator bool() const;
24  void operator()();
25  void reset();
26  bool sleep_for(std::chrono::milliseconds rel_time);
27  bool sleep_for(std::chrono::seconds rel_time);
28  bool sleep_for(std::chrono::minutes rel_time);
29 
30 private:
31  std::condition_variable cond;
33  std::atomic<bool> flag;
34 };
35 
36 #endif //BITCOIN_THREADINTERRUPT_H
CThreadInterrupt::flag
std::atomic< bool > flag
Definition: threadinterrupt.h:33
CThreadInterrupt::sleep_for
bool sleep_for(std::chrono::milliseconds rel_time)
Definition: threadinterrupt.cpp:31
sync.h
CThreadInterrupt::cond
std::condition_variable cond
Definition: threadinterrupt.h:31
AnnotatedMixin< std::mutex >
CThreadInterrupt::mut
Mutex mut
Definition: threadinterrupt.h:32
CThreadInterrupt::CThreadInterrupt
CThreadInterrupt()
Definition: threadinterrupt.cpp:10
CThreadInterrupt::operator()
void operator()()
Definition: threadinterrupt.cpp:22
CThreadInterrupt::reset
void reset()
Definition: threadinterrupt.cpp:17
CThreadInterrupt
Definition: threadinterrupt.h:19