SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
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 ()
 
 RWTThreadLocal (RWStaticCtor)
 
 ~RWTThreadLocal ()
 
Type & getValue () const
 
bool isSet (void) const
 
 operator Type (void) 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 (const RWTMonitor< RWMutexLock > &second)
 
 RWTMonitor (RWStaticCtor)
 
 RWTMonitor (void)
 
 ~RWTMonitor (void)
 
void acquire (void)
 
bool isAcquired (void) const
 
RWTMonitor< RWMutexLock > & monitor (void) const
 
RWMutexLockmutex (void)
 
RWTMonitor< RWMutexLock > & operator= (const RWTMonitor< RWMutexLock > &)
 
void release (void)
 
bool tryAcquire (void)
 

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/thread/RWTThreadLocal.h>
#include <rw/thread/RWThreadFunction.h>
#include <rw/sync/RWSemaphore.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;
}
Offers powerful and convenient facilities for manipulating strings.
Definition stdcstring.h:826
RWCompletionState start()
void join(void)
A synchronization object that maintains a non-negative counter.
Definition RWSemaphore.h:172
void acquire()
void release()
Provides thread-local storage with simple by-value semantics.
Definition RWTThreadLocal.h:212
Handle class for functor-based threaded runnable objects.
Definition RWThreadFunction.h:90
static RWThreadFunction make()

Constructor & Destructor Documentation

◆ RWTThreadLocal() [1/3]

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.

◆ RWTThreadLocal() [2/3]

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.

◆ ~RWTThreadLocal()

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.

◆ RWTThreadLocal() [3/3]

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

Copy construction prohibited.

Member Function Documentation

◆ getValue()

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

Retrieves a reference to the current value of this variable.

◆ isSet()

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

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

◆ operator Type()

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

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

◆ operator=() [1/2]

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

Assignment prohibited.

◆ operator=() [2/2]

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.

◆ reset()

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

Resets the value of this object.

◆ setValue()

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

Sets the value of this object.

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