Bitcoin Core 30.99.0
P2P Digital Currency
Public Types | Public Member Functions | Private Attributes | List of all members
CThreadInterrupt Class Reference

A helper class for interruptible sleeps. More...

#include <threadinterrupt.h>

Inheritance diagram for CThreadInterrupt:
[legend]
Collaboration diagram for CThreadInterrupt:
[legend]

Public Types

using Clock = std::chrono::steady_clock
 

Public Member Functions

 CThreadInterrupt ()
 
virtual ~CThreadInterrupt ()=default
 
virtual bool interrupted () const
 Return true if operator()() has been called. More...
 
virtual operator bool () const
 An alias for interrupted(). More...
 
virtual void operator() () EXCLUSIVE_LOCKS_REQUIRED(!mut)
 Interrupt any sleeps. After this interrupted() will return true. More...
 
virtual void reset ()
 Reset to an non-interrupted state. More...
 
virtual bool sleep_for (Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
 Sleep for the given duration. More...
 

Private Attributes

std::condition_variable cond
 
Mutex mut
 
std::atomic< bool > flag
 

Detailed Description

A helper class for interruptible sleeps.

Calling operator() will interrupt any current sleep, and after that point operator bool() will return true until reset.

This class should not be used in a signal handler. It uses thread synchronization primitives that are not safe to use with signals. If sending an interrupt from a signal handler is necessary, the SignalInterrupt class can be used instead.

Definition at line 26 of file threadinterrupt.h.

Member Typedef Documentation

◆ Clock

using CThreadInterrupt::Clock = std::chrono::steady_clock

Definition at line 29 of file threadinterrupt.h.

Constructor & Destructor Documentation

◆ CThreadInterrupt()

CThreadInterrupt::CThreadInterrupt ( )

Definition at line 10 of file threadinterrupt.cpp.

◆ ~CThreadInterrupt()

virtual CThreadInterrupt::~CThreadInterrupt ( )
virtualdefault

Member Function Documentation

◆ interrupted()

bool CThreadInterrupt::interrupted ( ) const
virtual

Return true if operator()() has been called.

Reimplemented in FuzzedThreadInterrupt.

Definition at line 12 of file threadinterrupt.cpp.

◆ operator bool()

CThreadInterrupt::operator bool ( ) const
explicitvirtual

An alias for interrupted().

Definition at line 17 of file threadinterrupt.cpp.

◆ operator()()

void CThreadInterrupt::operator() ( )
virtual

Interrupt any sleeps. After this interrupted() will return true.

Definition at line 27 of file threadinterrupt.cpp.

◆ reset()

void CThreadInterrupt::reset ( )
virtual

Reset to an non-interrupted state.

Definition at line 22 of file threadinterrupt.cpp.

Here is the caller graph for this function:

◆ sleep_for()

bool CThreadInterrupt::sleep_for ( Clock::duration  rel_time)
virtual

Sleep for the given duration.

Return values
trueThe time passed.
falseThe sleep was interrupted.

Reimplemented in FuzzedThreadInterrupt.

Definition at line 36 of file threadinterrupt.cpp.

Here is the caller graph for this function:

Member Data Documentation

◆ cond

std::condition_variable CThreadInterrupt::cond
private

Definition at line 53 of file threadinterrupt.h.

◆ flag

std::atomic<bool> CThreadInterrupt::flag
private

Definition at line 55 of file threadinterrupt.h.

◆ mut

Mutex CThreadInterrupt::mut
private

Definition at line 54 of file threadinterrupt.h.


The documentation for this class was generated from the following files: