JUCE  v6.1.6 (6.0.8-1114)
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::ThreadLocalValue< Type > Class Template Reference

Provides cross-platform support for thread-local objects. More...

#include <juce_ThreadLocalValue.h>

Collaboration diagram for juce::ThreadLocalValue< Type >:

Classes

struct  ObjectHolder
 

Public Member Functions

 ThreadLocalValue ()=default
 
 ~ThreadLocalValue ()
 Destructor. More...
 
Type & get () const noexcept
 Returns a reference to this thread's instance of the value. More...
 
 operator Type * () const noexcept
 Returns a pointer to this thread's instance of the value. More...
 
Type & operator* () const noexcept
 Returns a reference to this thread's instance of the value. More...
 
Type * operator-> () const noexcept
 Accesses a method or field of the value object. More...
 
ThreadLocalValueoperator= (const Type &newValue)
 Assigns a new value to the thread-local object. More...
 
void releaseCurrentThreadStorage ()
 Called by a thread before it terminates, to allow this class to release any storage associated with the thread. More...
 

Private Attributes

Atomic< ObjectHolder * > first
 

Detailed Description

template<typename Type>
class juce::ThreadLocalValue< Type >

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.

@tags{Core}

Constructor & Destructor Documentation

◆ ThreadLocalValue()

template<typename Type >
juce::ThreadLocalValue< Type >::ThreadLocalValue ( )
default

◆ ~ThreadLocalValue()

template<typename Type >
juce::ThreadLocalValue< Type >::~ThreadLocalValue ( )
inline

Destructor.

When this object is deleted, all the value objects for all threads will be deleted.

References juce::ThreadLocalValue< Type >::first.

Member Function Documentation

◆ get()

template<typename Type >
Type& juce::ThreadLocalValue< Type >::get ( ) const
inlinenoexcept

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 juce::Atomic< Type >::compareAndSetBool(), juce::ThreadLocalValue< Type >::first, juce::Atomic< Type >::get(), juce::Thread::getCurrentThreadId(), juce::ThreadLocalValue< Type >::ObjectHolder::next, juce::ThreadLocalValue< Type >::ObjectHolder::object, and juce::ThreadLocalValue< Type >::ObjectHolder::threadId.

Referenced by juce::ThreadLocalValue< Type >::operator Type *(), juce::ThreadLocalValue< Type >::operator*(), juce::ThreadLocalValue< Type >::operator->(), and juce::ThreadLocalValue< Type >::operator=().

◆ operator Type *()

template<typename Type >
juce::ThreadLocalValue< Type >::operator Type * ( ) const
inlinenoexcept

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 juce::ThreadLocalValue< Type >::get().

◆ operator*()

template<typename Type >
Type& juce::ThreadLocalValue< Type >::operator* ( ) const
inlinenoexcept

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 juce::ThreadLocalValue< Type >::get().

◆ operator->()

template<typename Type >
Type* juce::ThreadLocalValue< Type >::operator-> ( ) const
inlinenoexcept

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 juce::ThreadLocalValue< Type >::get().

◆ operator=()

template<typename Type >
ThreadLocalValue& juce::ThreadLocalValue< Type >::operator= ( const Type &  newValue)
inline

Assigns a new value to the thread-local object.

References juce::ThreadLocalValue< Type >::get().

◆ releaseCurrentThreadStorage()

template<typename Type >
void juce::ThreadLocalValue< Type >::releaseCurrentThreadStorage ( )
inline

Called by a thread before it terminates, to allow this class to release any storage associated with the thread.

References juce::ThreadLocalValue< Type >::first, and juce::Thread::getCurrentThreadId().

Member Data Documentation

◆ first


The documentation for this class was generated from the following file: