OpenWalnut  1.5.0dev
Public Member Functions | Private Member Functions | Private Attributes | List of all members
WCounter Class Reference

This is a simple but thread-safe counter. More...

#include <WCounter.h>

+ Collaboration diagram for WCounter:

Public Member Functions

 WCounter ()
 Constructor. More...
 
int operator++ ()
 Increase count by 1 and return the new counter value. More...
 
int operator-- ()
 Decrease count by 1 and return the new counter value. More...
 
int operator() () const
 Get current count. More...
 
int reset ()
 Reset the counter to 0. More...
 

Private Member Functions

 WCounter (WCounter &)
 No copy construction. More...
 
WCounter operator= (WCounter &)
 No copy operator. More...
 

Private Attributes

std::shared_mutex m_counterMutex
 A mutex to protect the counter from concurrent updates. More...
 
int m_counter
 The counter. More...
 

Detailed Description

This is a simple but thread-safe counter.

Use it to handle counting between multiple threads. When used as static member, you can utilized WCounter for instance counting.

Definition at line 36 of file WCounter.h.

Constructor & Destructor Documentation

◆ WCounter() [1/2]

WCounter::WCounter ( )
inline

Constructor.

Counter starts at 0.

Definition at line 42 of file WCounter.h.

◆ WCounter() [2/2]

WCounter::WCounter ( WCounter )
private

No copy construction.

Member Function Documentation

◆ operator()()

int WCounter::operator() ( ) const
inline

Get current count.

Returns
the current count.

Definition at line 75 of file WCounter.h.

References m_counter, and m_counterMutex.

◆ operator++()

int WCounter::operator++ ( )
inline

Increase count by 1 and return the new counter value.

This is threadsafe.

Returns
The new counter value.

Definition at line 53 of file WCounter.h.

References m_counter, and m_counterMutex.

◆ operator--()

int WCounter::operator-- ( )
inline

Decrease count by 1 and return the new counter value.

This is threadsafe.

Returns
The new counter value.

Definition at line 64 of file WCounter.h.

References m_counter, and m_counterMutex.

◆ operator=()

WCounter WCounter::operator= ( WCounter )
private

No copy operator.

Returns
Nothing.

◆ reset()

int WCounter::reset ( )
inline

Reset the counter to 0.

Returns
The value the counter had before the reset.

Definition at line 86 of file WCounter.h.

References m_counter, and m_counterMutex.

Member Data Documentation

◆ m_counter

int WCounter::m_counter
private

The counter.

Definition at line 109 of file WCounter.h.

Referenced by operator()(), operator++(), operator--(), and reset().

◆ m_counterMutex

std::shared_mutex WCounter::m_counterMutex
mutableprivate

A mutex to protect the counter from concurrent updates.

Definition at line 106 of file WCounter.h.

Referenced by operator()(), operator++(), operator--(), and reset().


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