base::PerThreadPtr< T > Class Template Reference

Magic pointer to per-thread object (thread local storage). More...

#include <base_thread_per.hh>

List of all members.

Public Member Functions

 PerThreadPtr (void)
 ~PerThreadPtr ()
T * operator-> (void)
 
Returns:
NULL if key invalid.

T & operator * (void)
 
Returns:
Reference to object.

T * PTR (void)
 Limited-use. Explicitly get raw pointer.

Private Member Functions

T * NewPerThreadVar (void)

Private Attributes

pthread_key_t mKey


Detailed Description

template<typename T>
class base::PerThreadPtr< T >

Magic pointer to per-thread object (thread local storage).

This class automatically allocates per-thread objects on the heap. The pointed-to class must have a default constructor.

PerThreadPtr is intended to be a class member. PerThreadPtr can be thought as a magic pointer that makes a new object local to a new thread.

Example: The 4 threads will animate 4 separate Ghosts. PerThreadPtr will detect that a new thread has started, and will allocate 4 Ghosts on the heap.

class Pacman { Pacman( void ) { RunThread(); RunThread(); RunThread(); RunThread(); } ~Pacman() { } CLASS_METHOD void RunThread( void ) { mGhost->Animate(); } PerThreadPtr<Ghost> mGhost; };

pthreads notes: pthreads just provides mapping between a thread and a POINTER. The application (this code) is responsible for allocating memory blocks on the heap for every thread. The Thread class allocates one large memory block for a new thread and deletes the whole block when the thread ends. PerThreadPtr sub-allocates the block and places new per-thread objects using C++ placement new.


Constructor & Destructor Documentation

template<typename T>
base::PerThreadPtr< T >::PerThreadPtr ( void   )  [inline]

template<typename T>
base::PerThreadPtr< T >::~PerThreadPtr (  )  [inline]


Member Function Documentation

template<typename T>
T* base::PerThreadPtr< T >::NewPerThreadVar ( void   )  [inline, private]

template<typename T>
T* base::PerThreadPtr< T >::operator-> ( void   )  [inline]

Returns:
NULL if key invalid.

template<typename T>
T& base::PerThreadPtr< T >::operator * ( void   )  [inline]

Returns:
Reference to object.

template<typename T>
T* base::PerThreadPtr< T >::PTR ( void   )  [inline]

Limited-use. Explicitly get raw pointer.


Member Data Documentation

template<typename T>
pthread_key_t base::PerThreadPtr< T >::mKey [private]


The documentation for this class was generated from the following file: Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:20 2007