Provides cross-platform support for thread-local objects. More...
#include <juce_ThreadLocalValue.h>
Public Member Functions | |
ThreadLocalValue ()=default | |
~ThreadLocalValue () | |
Destructor. | |
Type & | operator* () const noexcept |
Returns a reference to this thread's instance of the value. | |
operator Type * () const noexcept | |
Returns a pointer to this thread's instance of the value. | |
Type * | operator-> () const noexcept |
Accesses a method or field of the value object. | |
ThreadLocalValue & | operator= (const Type &newValue) |
Assigns a new value to the thread-local object. | |
Type & | get () const noexcept |
Returns a reference to this thread's instance of the value. | |
void | releaseCurrentThreadStorage () |
Called by a thread before it terminates, to allow this class to release any storage associated with the thread. | |
Provides cross-platform support for thread-local objects.
This class holds an internal list of objects of the templated type, keeping an instance for each thread that requests one. The first time a thread attempts to access its value, an object is created and added to the list for that thread.
Typically, you'll probably want to create a static instance of a ThreadLocalValue object, or hold one within a singleton.
The templated class for your value must be a primitive type, or a simple POD struct.
When a thread no longer needs to use its value, it can call releaseCurrentThreadStorage() to allow the storage to be re-used by another thread. If a thread exits without calling this method, the object storage will be left allocated until the ThreadLocalValue object is deleted.
|
default |
ThreadLocalValue< Type >::~ThreadLocalValue | ( | ) |
Destructor.
When this object is deleted, all the value objects for all threads will be deleted.
References Atomic< Type >::get().
|
noexcept |
Returns a reference to this thread's instance of the value.
Note that the first time a thread tries to access the value, an instance of the value object will be created - so if your value's class has a non-trivial constructor, be aware that this method could invoke it.
References ThreadLocalValue< Type >::get().
|
noexcept |
Returns a pointer to this thread's instance of the value.
Note that the first time a thread tries to access the value, an instance of the value object will be created - so if your value's class has a non-trivial constructor, be aware that this method could invoke it.
References ThreadLocalValue< Type >::get().
|
noexcept |
Accesses a method or field of the value object.
Note that the first time a thread tries to access the value, an instance of the value object will be created - so if your value's class has a non-trivial constructor, be aware that this method could invoke it.
References ThreadLocalValue< Type >::get().
ThreadLocalValue & ThreadLocalValue< Type >::operator= | ( | const Type & | newValue | ) |
Assigns a new value to the thread-local object.
References ThreadLocalValue< Type >::get().
|
noexcept |
Returns a reference to this thread's instance of the value.
Note that the first time a thread tries to access the value, an instance of the value object will be created - so if your value's class has a non-trivial constructor, be aware that this method could invoke it.
References Atomic< Type >::compareAndSetBool(), Atomic< Type >::get(), and Thread::getCurrentThreadId().
Referenced by ThreadLocalValue< Type >::operator Type *(), ThreadLocalValue< Type >::operator*(), ThreadLocalValue< Type >::operator->(), and ThreadLocalValue< Type >::operator=().
void ThreadLocalValue< Type >::releaseCurrentThreadStorage | ( | ) |
Called by a thread before it terminates, to allow this class to release any storage associated with the thread.
References Atomic< Type >::get(), and Thread::getCurrentThreadId().