Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2016-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_THREADINTERRUPT_H6#define BITCOIN_UTIL_THREADINTERRUPT_H8#include <sync.h>9#include <util/time.h>11#include <atomic>12#include <condition_variable>14/**15* A helper class for interruptible sleeps. Calling operator() will interrupt16* any current sleep, and after that point operator bool() will return true17* until reset.18*19* This class should not be used in a signal handler. It uses thread20* synchronization primitives that are not safe to use with signals. If sending21* an interrupt from a signal handler is necessary, the \ref SignalInterrupt22* class can be used instead.23*/25class CThreadInterrupt26{27public:28using Clock = std::chrono::steady_clock;30CThreadInterrupt();32virtual ~CThreadInterrupt() = default;34/// Return true if `operator()()` has been called.35virtual bool interrupted() const;37/// An alias for `interrupted()`.38virtual explicit operator bool() const;40/// Interrupt any sleeps. After this `interrupted()` will return `true`.41virtual void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut);43/// Reset to an non-interrupted state.44virtual void reset();46/// Sleep for the given duration.47/// @retval true The time passed.48/// @retval false The sleep was interrupted.49virtual bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);51private:52std::condition_variable cond;53Mutex mut;54std::atomic<bool> flag;55};57#endif // BITCOIN_UTIL_THREADINTERRUPT_H