Bitcoin Core  24.99.0
P2P Digital Currency
threadinterrupt.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2022 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_UTIL_THREADINTERRUPT_H
6 #define BITCOIN_UTIL_THREADINTERRUPT_H
7 
8 #include <sync.h>
9 #include <threadsafety.h>
10 
11 #include <atomic>
12 #include <chrono>
13 #include <condition_variable>
14 
15 /*
16  A helper class for interruptible sleeps. Calling operator() will interrupt
17  any current sleep, and after that point operator bool() will return true
18  until reset.
19 */
21 {
22 public:
23  using Clock = std::chrono::steady_clock;
25  explicit operator bool() const;
27  void reset();
28  bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
29 
30 private:
31  std::condition_variable cond;
33  std::atomic<bool> flag;
34 };
35 
36 #endif // BITCOIN_UTIL_THREADINTERRUPT_H
std::chrono::steady_clock Clock
void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut)
std::atomic< bool > flag
bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
std::condition_variable cond
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49