Raven Core  3.0.0
P2P Digital Currency
threadinterrupt.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "threadinterrupt.h"
8 
9 CThreadInterrupt::operator bool() const
10 {
11  return flag.load(std::memory_order_acquire);
12 }
13 
15 {
16  flag.store(false, std::memory_order_release);
17 }
18 
20 {
21  {
22  std::unique_lock<std::mutex> lock(mut);
23  flag.store(true, std::memory_order_release);
24  }
25  cond.notify_all();
26 }
27 
28 bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
29 {
30  std::unique_lock<std::mutex> lock(mut);
31  return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
32 }
33 
34 bool CThreadInterrupt::sleep_for(std::chrono::seconds rel_time)
35 {
36  return sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
37 }
38 
39 bool CThreadInterrupt::sleep_for(std::chrono::minutes rel_time)
40 {
41  return sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
42 }
bool sleep_for(std::chrono::milliseconds rel_time)
std::condition_variable cond
std::atomic< bool > flag