RWTThreadLocal<Type> RWTMonitor<RWMutexLock>
#include <rw/thread/RWTThreadLocal.h>
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.
#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; }
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(void);
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.
~RWTThreadLocal(void);
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.
RWTThreadLocal<Type>& operator=(const Type& value);
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.
operator Type(void) const;
Retrieves and returns the value previously stored by the current thread.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.