diff options
author | Cory Fields <cory-nospam-@coryfields.com> | 2016-12-27 17:12:15 -0500 |
---|---|---|
committer | Cory Fields <cory-nospam-@coryfields.com> | 2017-01-03 17:53:09 -0500 |
commit | 799df9115f262fbc25c2c2737ccd8a4e1b20e5b0 (patch) | |
tree | 6f146cd1c5ae50626955b2b31de244d0a746ba4c /src/threadinterrupt.h | |
parent | 7325b1556684158461c9c0df056c9d071444b54e (diff) |
net: add CThreadInterrupt and InterruptibleSleep
Diffstat (limited to 'src/threadinterrupt.h')
-rw-r--r-- | src/threadinterrupt.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/threadinterrupt.h b/src/threadinterrupt.h new file mode 100644 index 0000000000..54e3102808 --- /dev/null +++ b/src/threadinterrupt.h @@ -0,0 +1,34 @@ +// Copyright (c) 2016 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_THREADINTERRUPT_H +#define BITCOIN_THREADINTERRUPT_H + +#include <atomic> +#include <chrono> +#include <condition_variable> +#include <mutex> + +/* + A helper class for interruptible sleeps. Calling operator() will interrupt + any current sleep, and after that point operator bool() will return true + until reset. +*/ +class CThreadInterrupt +{ +public: + explicit operator bool() const; + void operator()(); + void reset(); + bool sleep_for(std::chrono::milliseconds rel_time); + bool sleep_for(std::chrono::seconds rel_time); + bool sleep_for(std::chrono::minutes rel_time); + +private: + std::condition_variable cond; + std::mutex mut; + std::atomic<bool> flag; +}; + +#endif //BITCOIN_THREADINTERRUPT_H |