SourcePro® API Reference Guide

Product Documentation:
   SourcePro
Documentation Home
List of all members | Public Member Functions | Private Member Functions
RWTThreadLocal< Type > Class Template Reference

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

#include <rw/thread/RWTThreadLocal.h>

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

Public Member Functions

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

Private Member Functions

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

Additional Inherited Members

- Protected Types inherited from RWTMonitor< RWMutexLock >
typedef RWTLockGuard< RWTMonitor< RWMutexLock > > LockGuard
 
typedef RWTTryLockGuard< RWTMonitor< RWMutexLock > > TryLockGuard
 
typedef RWTUnlockGuard< RWTMonitor< RWMutexLock > > UnlockGuard
 
- Protected Member Functions inherited from RWTMonitor< RWMutexLock >
 RWTMonitor ()
 
 RWTMonitor (RWStaticCtor)
 
 RWTMonitor (const RWTMonitor< RWMutexLock > &second)
 
 ~RWTMonitor ()
 
void acquire ()
 
bool isAcquired () const
 
RWTMonitor< RWMutexLock > & monitor () const
 
RWMutexLockmutex ()
 
RWTMonitor< RWMutexLock > & operator= (const RWTMonitor< RWMutexLock > &)
 
void release ()
 
bool tryAcquire ()
 

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.

Example
#include <rw/sync/RWSemaphore.h>
#include <rw/thread/RWThreadFunction.h>
#include <rw/thread/RWTThreadLocal.h>
#include <rw/cstring.h>
RWTThreadLocal<int> threadLocalVal;
RWCString nonThreadLocalString;
RWSemaphore sema1; //Used to synchronize
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
std::cout << "Thread's val: " << threadLocalVal << std::endl;
std::cout << "Thread's string: " << nonThreadLocalString << std::endl;
}
int main()
{
RWThreadFunction myThread = RWThreadFunction::make(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
std::cout << "Main's val: " << threadLocalVal << std::endl;
std::cout << "Main's string: " << nonThreadLocalString << std::endl;
sema1.release(); //Tell other thread to print values
myThread.join(); //Wait for other thread to end
return 0;
}

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 references 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, they necessarily are destroyed during the program termination process. Any threads that may still be running during program termination must guard against trying to access these storage objects.

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< Type > & RWTThreadLocal< Type >::operator= ( const Type &  value)
inline

Assigns a Type value to self. The value assigned is available only to the current thread. Access of self by other threads manipulate the values stored specifically for those threads.

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

Assignment prohibited.

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

Resets the value of this object.

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

Sets the value of this object.

Copyright © 2021 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.