C12Adapter Opensource C++ Interface
MInterlocked Class Reference

System independent lightweight synchronization object that synchronizes the access to one variable across multiple threads. More...

Public Types

typedef int ValueType
 Type used by interlocked class by default.
 

Public Member Functions

 MInterlocked (int value=0)
 Constructor of the interlocked variable. More...
 
 MInterlocked (const MInterlocked &other)
 Copy constructor that copies value from parameter. More...
 
MInterlockedoperator= (int value)
 Assignment operator that takes integer value. More...
 
MInterlockedoperator= (const MInterlocked &other)
 Object assignment operator. More...
 
 operator int () const
 Access the value of the interlocked class. More...
 
int operator++ ()
 Atomically increment this object value and return result. More...
 
int operator++ (int)
 Atomically increment this object value and return previous value. More...
 
int operator-- ()
 Atomically decrement this object value and return result. More...
 
int operator-- (int)
 Atomically decrement this object value and return previous value. More...
 

Static Public Member Functions

static int IncrementAndFetch (volatile int *v)
 Static function that atomically increments an integer in the given pointer and returns the new value. More...
 
static int FetchAndIncrement (volatile int *v)
 Static function that atomically increments an integer in the given pointer and returns the previous value. More...
 
static int DecrementAndFetch (volatile int *v)
 Static function that atomically decrements an integer in the given pointer and returns the new value. More...
 
static int FetchAndDecrement (volatile int *v)
 Static function that atomically decrements an integer in the given pointer and returns the previous value. More...
 

Detailed Description

System independent lightweight synchronization object that synchronizes the access to one variable across multiple threads.

At present, the only atomic operations supported are increment and decrement.

Implementation note: On the majority of architectures, assignment to and from a properly aligned unsigned variable is an atomic operation.

Constructor & Destructor Documentation

MInterlocked::MInterlocked ( int  value = 0)
inline

Constructor of the interlocked variable.

Parameters
valueInitial value of interlocked variable
MInterlocked::MInterlocked ( const MInterlocked other)
inline

Copy constructor that copies value from parameter.

If during this operation the value of the other object keeps changing, the result value will be consistent, correspond to some value that was held by other object at an unknown time of the copy operation.

Parameters
otherInterlocked object from which to copy value

Member Function Documentation

static int MInterlocked::DecrementAndFetch ( volatile int *  v)
inlinestatic

Static function that atomically decrements an integer in the given pointer and returns the new value.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Parameters
vVolatile pointer to the integer that should be decremented
Returns
integer value that is the result of the operation.
static int MInterlocked::FetchAndDecrement ( volatile int *  v)
inlinestatic

Static function that atomically decrements an integer in the given pointer and returns the previous value.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Parameters
vVolatile pointer to the integer that should be decremented
Returns
integer value that had previously been in memory.
static int MInterlocked::FetchAndIncrement ( volatile int *  v)
inlinestatic

Static function that atomically increments an integer in the given pointer and returns the previous value.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Parameters
vVolatile pointer to the integer that should be incremented
Returns
integer value that had previously been in memory.
static int MInterlocked::IncrementAndFetch ( volatile int *  v)
inlinestatic

Static function that atomically increments an integer in the given pointer and returns the new value.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Parameters
vVolatile pointer to the integer that should be incremented
Returns
integer value that is the result of the operation.
MInterlocked::operator int ( ) const
inline

Access the value of the interlocked class.

If the object keeps changing from other threads, the result will be a consistent value at some definite but unknown period of time.

int MInterlocked::operator++ ( )
inline

Atomically increment this object value and return result.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Returns
integer value that is the result of the operation.
int MInterlocked::operator++ ( int  )
inline

Atomically increment this object value and return previous value.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Returns
integer value that had previously been in memory.
int MInterlocked::operator-- ( )
inline

Atomically decrement this object value and return result.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Returns
integer value that is the result of the operation.
int MInterlocked::operator-- ( int  )
inline

Atomically decrement this object value and return previous value.

This operation should be used in multithreaded environments in order to prevent situations when multiple threads would fail to change the same value in a consistent way.

Returns
integer value that had previously been in memory.
MInterlocked& MInterlocked::operator= ( int  value)
inline

Assignment operator that takes integer value.

When many threads attempt to change interlocked value at the same time, any one can be the last, and the result value will be atomically changed.

Parameters
valueValue of interlocked variable
MInterlocked& MInterlocked::operator= ( const MInterlocked other)
inline

Object assignment operator.

If during this operation the value of the other object keeps changing, the result value will be consistent, correspond to some value that was held by the other object at an unknown time of the copy operation. If during this operation the value of this object keeps changing with other assignment operators, the result of the copy can be discarded, however if increment or decrement operations are used, the increments will start to work on the new value.

Parameters
otherInterlocked object from which to copy value