#include <ThreadLocal.h>
Inheritance diagram for ZThread::ThreadLocal< T, InitialValueT, ChildValueT, InheritableValueT >:
Public Member Functions | |
T | get () const |
void | set (T v) const |
void | clear () const |
Static Public Member Functions | |
static void | clearAll () |
The first time a thread accesses the value associated with a thread local storage context, a value is created. That value is either an initial value (determined by InitialValueT) or an inherited value (determined by ChildValueT).
Not all ThreadLocal's support the inheritance of values from parent threads. The default behavoir is to create values through the InitialValueT functor for all thread when they first access a thread local storage context.
A ThreadLocal that does not inherit, and uses the default value for an int as its initial value.
#include "zthread/ThreadLocal.h" #include "zthread/Thread.h" #include <iostream> using namespace ZThread; class aRunnable : public Runnable { ThreadLocal<int> localValue; public: void run() { std::cout << localValue.get() << std::endl; } }; int main() { // Create a shared task to display ThreadLocal values Task task(new aRunnable); Thread t0(task); t0.wait(); Thread t1(task); t1.wait(); Thread t2(task); t2.wait(); // Output: // 0 // 0 // 0 return 0; }
A ThreadLocal that does not inherit, and uses a custom initial value.
#include "zthread/ThreadLocal.h" #include "zthread/Thread.h" #include <iostream> using namespace ZThread; struct anInitialValueFn { int operator()() { static int next = 100; int val = next; next += 100; return val; } }; class aRunnable : public Runnable { ThreadLocal<int, anInitialValueFn> localValue; public: void run() { std::cout << localValue.get() << std::endl; } }; int main() { // Create a shared task to display ThreadLocal values Task task(new aRunnable); Thread t0(task); t0.wait(); Thread t1(task); t1.wait(); Thread t2(task); t2.wait(); // Output: // 100 // 200 // 300 return 0; }
A ThreadLocal that does inherit and modify child values. (The default initial value functor is used)
#include "zthread/ThreadLocal.h" #include "zthread/Thread.h" #include <iostream> using namespace ZThread; struct anInheritedValueFn { int operator()(int val) { return val + 100; } }; // This Runnable associates no ThreadLocal value in the main thread; so // none of the child threads have anything to inherit. class aRunnable : public Runnable { ThreadLocal<int, ThreadLocalImpl::InitialValueFn<int>, anInheritedValueFn> localValue; public: void run() { std::cout << localValue.get() << std::endl; } }; // This Runnable associates a ThreadLocal value in the main thread which // is inherited when the child threads are created. class anotherRunnable : public Runnable { ThreadLocal<int, ThreadLocalImpl::InitialValueFn<int>, anInheritedValueFn> localValue; public: anotherRunnable() { localValue.set(100); } void run() { std::cout << localValue.get() << std::endl; } }; int main() { // Create a shared task to display ThreadLocal values Task task(new aRunnable); Thread t0(task); t0.wait(); Thread t1(task); t1.wait(); Thread t2(task); t2.wait(); // Output: // 0 // 0 // 0 task = Task(new anotherRunnable); Thread t10(task); t10.wait(); Thread t11(task); t11.wait(); Thread t12(task); t12.wait(); // Output: // 200 // 200 // 200 return 0; }
InitialValueT
This template parameter should indicate the functor used to set the initial value. It should support the following operator:
// required operator T operator()
// supported expression InitialValueT()()
ChildValueT
This template parameter should indicate the functor used to set the value that will be inherited by thread whose parent have associated a value with the ThreadLocal's context at the time they are created. It should support the following operator:
// required operator T operator(const T& parentValue)
// supported expression ChildValueT()(parentValue)
InheritableValueT
This template parameter should indicate the functor, used to determine wheather or not this ThreadLocal will allow values from a parent threads context to be inherited by child threads when they are created. It should support the following operator:
// required operator bool operator(const T& childValueFunctor)
// supported expression InheritableValueT()( ChildValueT() )
void ZThread::ThreadLocal< T, InitialValueT, ChildValueT, InheritableValueT >::clear | ( | ) | const [inline] |
Remove any value current associated with this ThreadLocal.
Reimplemented from ZThread::ThreadLocalImpl.
static void ZThread::ThreadLocal< T, InitialValueT, ChildValueT, InheritableValueT >::clearAll | ( | ) | [inline, static] |
Remove any value current associated with any ThreadLocal.
Reimplemented from ZThread::ThreadLocalImpl.
T ZThread::ThreadLocal< T, InitialValueT, ChildValueT, InheritableValueT >::get | ( | ) | const [inline] |
Get the value associated with the context (this ThreadLocal and the calling thread) of the invoker. If no value is currently associated, then an intial value is created and associated; that value is returned.
void ZThread::ThreadLocal< T, InitialValueT, ChildValueT, InheritableValueT >::set | ( | T | v | ) | const [inline] |
Replace the value associated with the context (this ThreadLocal and the calling thread) of the invoker. If no value is currently associated, then an intial value is first created and subsequently replaced by the new value.
v | value of type T to associate. |