rwlogo
SourcePro 11.1

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWTThreadLocal< Type > Class Template Reference
[Threading]

Provides thread-local storage with simple by-value semantics. More...

#include <rw/thread/RWTThreadLocal.h>

Inheritance diagram for RWTThreadLocal< Type >:
RWTMonitor< RWMutexLock >

List of all members.

Public Types

typedef RWTLockGuard
< RWTMonitor< RWMutexLock > > 
LockGuard
typedef RWTUnlockGuard
< RWTMonitor< RWMutexLock > > 
UnlockGuard
typedef RWTTryLockGuard
< RWTMonitor< RWMutexLock > > 
TryLockGuard

Public Member Functions

 RWTThreadLocal (RWStaticCtor)
 RWTThreadLocal ()
 ~RWTThreadLocal ()
RWTThreadLocal< Type > & operator= (const Type &value)
 operator Type () const
bool isSet () const
Type & getValue () const
void setValue (const Type &value)
bool reset (void)

Private Member Functions

 RWTThreadLocal (const RWTThreadLocal &second)
RWTThreadLocaloperator= (const RWTThreadLocal &second)

Detailed Description

template<class Type>
class RWTThreadLocal< Type >

The RWTThreadLocal class provides thread-local storage with simple by-value semantics. An RWTThreadLocal instance may be shared between multiple threads. Doing so creates one instance of typed data per thread. Each thread accesses its own private instance of the typed data whenever it uses the shared RWTThreadLocal instance.

Examples

 #include<rw/thread/RWTThreadLocal.h>
 #include<rw/cstring.h>
 
 RWTThreadLocal<int> threadLocalVal;
 RWCString nonThreadLocalString;
 
 RWSemaphore sema1;    //Used to synchronize
 RWSemaphore sema2;
 
 void multiThreadFunc(){
   sema1.acquire();  //Wait for main thread to set values
 
   //Set global variables to our own values
   threadLocalVal = 5;
   nonThreadLocalString = "Ha Ha I overwrote Main's string";
 
   sema2.release(); //Tell main thread to print out its values
   sema1.acquire(); //Wait for main thread to print
 
   //Print out our values
   cout << "Thread's val: " << threadLocalVal << endl;
   cout << "Thread's string: " << nonThreadLocalString << endl;
   }
 
 int main(){
   RWThreadFunction myThread =
     rwtMakeThreadFunction(multiThreadFunc);
 
   myThread.start();
 
   //Set main thread's values
   threadLocalVal = 10;
   nonThreadLocalString = "Main's String";
 
   sema1.release();  //Tell other thread to set values
   sema2.acquire();  //Wait for other thread to set values
 
   //Print out main thread's values
   cout << "Main's val: " << threadLocalVal << endl;
   cout << "Main's string: " << nonThreadLocalString << endl;
 
    sema1.release(); //Tell other thread to print values
 
   myThread.join(); //Wait for other thread to end
   return 0;
 }

Member Typedef Documentation

template<class Type>
typedef RWTLockGuard< RWTMonitor< RWMutexLock > > RWTThreadLocal< Type >::LockGuard

Predefined guard for use with monitor.

Reimplemented from RWTMonitor< RWMutexLock >.

template<class Type>
typedef RWTTryLockGuard< RWTMonitor< RWMutexLock > > RWTThreadLocal< Type >::TryLockGuard

Predefined guard for use with monitor.

Reimplemented from RWTMonitor< RWMutexLock >.

template<class Type>
typedef RWTUnlockGuard< RWTMonitor< RWMutexLock > > RWTThreadLocal< Type >::UnlockGuard

Predefined guard for use with monitor.

Reimplemented from RWTMonitor< RWMutexLock >.


Constructor & Destructor Documentation

template<class Type>
RWTThreadLocal< Type >::RWTThreadLocal ( RWStaticCtor   ) 

Constructs an RWTThreadLocal instance, which is initialized when first used, unlike an instance created by the default constructor, which is initialized when constructed.

template<class Type>
RWTThreadLocal< Type >::RWTThreadLocal (  ) 

Constructs and initializes a thread local storage object. The first time a thread accesses this object, a new instance of Type is created specifically for that thread. Each subsequent access by the same thread will refer to the same Type instance.

template<class Type>
RWTThreadLocal< Type >::~RWTThreadLocal (  ) 

Destroys the thread-local storage object. Since thread-local storage objects are often declared at global scope, you should take care that other threads do not try to access a thread-local storage instance after it has been destroyed during the program termination process.

template<class Type>
RWTThreadLocal< Type >::RWTThreadLocal ( const RWTThreadLocal< Type > &  second  )  [private]

Copy construction prohibited.


Member Function Documentation

template<class Type>
Type& RWTThreadLocal< Type >::getValue (  )  const

Retrieves a reference to the current value of this variable.

template<class Type >
bool RWTThreadLocal< Type >::isSet (  )  const [inline]

Returns true if this variable has been set in the current thread; otherwise, returns false.

template<class Type >
RWTThreadLocal< Type >::operator Type (  )  const [inline]

Retrieves and returns the value previously stored by the current thread.

template<class Type>
RWTThreadLocal& RWTThreadLocal< Type >::operator= ( const RWTThreadLocal< Type > &  second  )  [private]

Assignment prohibited.

template<class Type >
RWTThreadLocal< Type > & RWTThreadLocal< Type >::operator= ( const Type &  value  )  [inline]

Assigns a Type value to self. The value assigned will be available only to the current thread. Accesses to self by other threads will manipulate the values stored specifically for those threads.

template<class Type>
bool RWTThreadLocal< Type >::reset ( void   ) 

Resets the value.

template<class Type>
void RWTThreadLocal< Type >::setValue ( const Type &  value  ) 

Sets the value of this object.


© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.