#include <wefts_specific.h>
Collaboration diagram for Wefts::ThreadPtr< _T >:
This class abstracts the access to thread specific data encapsulated it in the semantics of a pointer. Laid down simply, you have here a pointer that will point to a different thing in different threads.
Usage is pretty simple; declare a template of the data type you need, and you have an object that acts with the semantics of a pointer to that type:
... ThreadPtr<int> status; int data; status = &data; *status = 2; // --> data == 2! status == &data; //--> TRUE! // have a way to send a status reference (or pointer, if you like) // to all the threads, i.e. MyThread mt1( status ); MyThread mt2( status ); MyThread mt3( status ); mt1.start(); mt2.start(); mt3.start(); ...
In the run() method of our threads you will assign the status to different object pointers; you will be able to use the same status reference, and the object that it is pointing will be different in each thread.
When a ThreadPtr is not initialized, it holds the value 0. Trying to access it using the * operator will throw a InvalidError.
Once the pointer is created (i.e. statically declared in one module, or allocated dynamically), a ThreadPtr should never be deleted; you should consider it as a "cargo" that may hold various things you may need, that once you ask for, stays until the end of the program.
Support for deleting this object is provided, but if you do so, you must make sure that there isn't any thread referencing anymore to this ThreadPtr, AND that all the data stored in the various threads is already cleaned. In fact, it is impossible for the thread that delete this object to access, and so, to clean the data stored by other threads.
The same is true also for derived classes or classes using this pointer.
A simple implementation can be using this ThreadPtr class as a member of a class derived from Referenced; you would then have an object that could hold some data common to all threads (and that you can guard with Referenced lock() calls), and some other data that is different in each thread:
class SmartRef: public Referenced { MyThing *common; ThreadPtr<MyThing> specific; ... };
Anyway, you have still to take care to manually delete the specific data.
Public Member Functions | |
ThreadPtr () | |
ThreadPtr (_T *data) | |
ThreadPtr (ThreadPtr< _T > &src) | |
_T * | get () const |
Gets the pointer that is stored in this object. | |
ThreadPtr< _T > & | set (ThreadPtr< _T > &src) |
Copies another pointer into this one. | |
_T * | set (_T *data) |
Set the data pointed by this thread. | |
bool | equal (const ThreadPtr< _T > &src) const |
Verifies if two ThreadPtr objects are equal. | |
bool | equal (const _T *data) const |
Verifies if a ThreadPtr is holding a certain data (in this thread). | |
_T & | operator * () |
ThreadPtr< _T > & | operator= (ThreadPtr< _T > &src) |
_T * | operator= (_T *data) |
bool | operator== (const ThreadPtr< _T > &src) const |
bool | operator== (const _T *data) const |
bool | operator!= (const ThreadPtr< _T > &src) const |
bool | operator!= (const _T *data) const |
Protected Member Functions | |
_T & | deref () throw ( InvalidError ) |
Dereference the internal pointer to obtain the object reference. | |
Private Attributes | |
OSSpecificData | m_specific |
Low level thread specific accessor. |
|
|
|
|
|
|
|
Dereference the internal pointer to obtain the object reference. If the internal pointer is null, that is, if the pointer has not yet been set, an InvalidError is thrown. |
|
Verifies if a ThreadPtr is holding a certain data (in this thread). Works also if the parameter is 0, returning true if the object is initialized. So, thptr == 0 is a valid way to determine if the object is in an invalid state. |
|
Verifies if two ThreadPtr objects are equal. Returns true only if the specifics are holding the SAME pointer. To see if two objects held in a ThreadPtr are equal, use the usual *ptra == *ptrb syntax. |
|
Gets the pointer that is stored in this object.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set the data pointed by this thread. The parameter may also be 0; this will reset this object to the invalid state. |
|
Copies another pointer into this one. Both of the pointers may be invalid (holding a null) when called. |
|
Low level thread specific accessor.
|