#include <queue.h>
Public Member Functions | |
| synchronised () | |
| Construct a wrapped race-condition-safe variable. | |
| synchronised (const T &val) | |
| synchronised (const synchronised< T > &ref) | |
| T | operator= (const T &val) |
| Assign a value (of the same type as T) to the object. | |
| T | 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. | |
| T | operator++ (int) |
| T | operator++ () |
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.
| karoo::synchronised< T >::synchronised | ( | ) | [inline] |
Construct a wrapped race-condition-safe variable.
| q | the queue whose runner thread is synchronised with accesses to this variable, |
| 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
| 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;
| 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;
1.5.8