karoo::synchronised< T > Class Template Reference

This templated class is for creating race-condition-safe variables. More...

#include <queue.h>

List of all members.

Public Member Functions

 synchronised ()
 Construct a wrapped race-condition-safe variable.
 synchronised (const T &val)
 synchronised (const synchronised< T > &ref)
operator= (const T &val)
 Assign a value (of the same type as T) to the object.
operator= (const synchronised< T > &ref)
 Assign a value (from another sybchronised<T> object of the same type T) to the object.
 operator T () const
 Get the value from the object.
operator++ (int)
operator++ ()


Detailed Description

template<typename T>
class karoo::synchronised< T >

This templated class is for creating race-condition-safe variables.

Any variables that need to be safely accessed from any thread, should be wrapped in this class.

E.g. to create a race-safe variable of type "double" that can be accessed immediately from any pebble run in any queue or pool:

 synchronised<double> x;
 synchronised<double> y;
 
 // e.g. to swap the values x and y
 double d = x;
 x = y;
 y = d;

WARNING: please don't use this mechanism "everywhere". Only use it where it is needed. Increasing the number of semaphores is not a good idea, and this type of variable is slower to access than a normal variable.

NOTE that this templated class does not pass references to the value, because obviously this would allow the memory to be accessed from outside the semaphore-protected block. So if you wrap a class with synchronised, then it must have efficient assignment operators.


Constructor & Destructor Documentation

template<typename T>
karoo::synchronised< T >::synchronised (  )  [inline]

Construct a wrapped race-condition-safe variable.

Parameters:
q the queue whose runner thread is synchronised with accesses to this variable,


Member Function Documentation

template<typename T>
karoo::synchronised< T >::operator T (  )  const [inline]

Get the value from the object.

Usually this works without needing to explicitly cast it. E.g.

 synchronised<int> sync;
 ...
    int i = sync;      // implicit cast
    i = (int)sync;     // identical to the above

template<typename T>
T karoo::synchronised< T >::operator= ( const synchronised< T > &  ref  )  [inline]

Assign a value (from another sybchronised<T> object of the same type T) to the object.

E.g.

 synchronised<int> sync;
 synchronised<int> another;
 ...
    sync = another;

template<typename T>
T karoo::synchronised< T >::operator= ( const T &  val  )  [inline]

Assign a value (of the same type as T) to the object.

E.g.

 synchronised<int> sync;
 ...
    sync = 10;


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

Generated on Tue Feb 16 15:04:29 2010 for Karoo by  doxygen 1.5.8