ThreadSafeRef< REFERENCE > Class Template Reference

#include <threadsaferef.h>

Inheritance diagram for ThreadSafeRef< REFERENCE >:

Detailed Description

template<typename REFERENCE>
class maxon::ThreadSafeRef< REFERENCE >

Template for safe access to BaseRef from multiple threads. This comes at the cost of extra atomic operations and will cause high contention if used thoughtless.

ThreadSafeRef<SampleClassRef> g_global;
// consumer thread
SampleClassRef now = g_global;
// producer thread
SampleClassRef newObject = CreateSomething();
g_global = newObject;
Note
If there is high contention a queue will perform much better (no matter if you use a job queue or a MPMC queue to store the objects only).
Template Parameters
REFERENCEType of the reference that will be stored thread-safe.

Public Member Functions

 ThreadSafeRef ()
 
 ThreadSafeRef (const REFERENCE &strongReference)
 
 ThreadSafeRef (const ThreadSafeRef &src)
 
 ThreadSafeRef (ThreadSafeRef &&src)
 
 ~ThreadSafeRef ()
 
ThreadSafeRefoperator= (const REFERENCE &strongReference)
 
Bool TryCompareAndSwap (const REFERENCE &strongReference, const REFERENCE &compare)
 
ThreadSafeRefoperator= (const ThreadSafeRef &src)
 
ThreadSafeRefoperator= (std::nullptr_t)
 
ThreadSafeRefoperator= (ThreadSafeRef &&src)
 
REFERENCE Get () const
 
 operator REFERENCE () const
 

Private Types

using DereferencedPtr = typename REFERENCE::ReferencedType *
 

Additional Inherited Members

- Protected Member Functions inherited from ThreadSafePtr< REFERENCE::ReferencedType * >
MAXON_IMPLICIT ThreadSafePtr (REFERENCE::ReferencedType * src=nullptr)
 
 ~ThreadSafePtr ()
 
REFERENCE::ReferencedType * AcquirePtr () const
 
void ReleasePtr (REFERENCE::ReferencedType * ptr) const
 
REFERENCE::ReferencedType * ChangePtr (REFERENCE::ReferencedType * ptr) const
 
Bool TryCompareAndSwapPtr (REFERENCE::ReferencedType * ptr, REFERENCE::ReferencedType * compare) const
 
REFERENCE::ReferencedType * AttemptAcquirePtr () const
 
- Protected Attributes inherited from ThreadSafePtr< REFERENCE::ReferencedType * >
AtomicUInt _ptr
 

Member Typedef Documentation

◆ DereferencedPtr

using DereferencedPtr = typename REFERENCE::ReferencedType*
private

Constructor & Destructor Documentation

◆ ThreadSafeRef() [1/4]

◆ ThreadSafeRef() [2/4]

ThreadSafeRef ( const REFERENCE &  strongReference)
explicit
Parameters
[in]strongReferenceBaseRef with strong reference to an object.

◆ ThreadSafeRef() [3/4]

ThreadSafeRef ( const ThreadSafeRef< REFERENCE > &  src)

Copy constructs a thread safe pointer. THREADSAFE.

Parameters
[in]srcSource thread safe pointer.

◆ ThreadSafeRef() [4/4]

ThreadSafeRef ( ThreadSafeRef< REFERENCE > &&  src)

Moves a thread safe pointer. THREADSAFE.

Parameters
[in]srcSource thread safe pointer.

◆ ~ThreadSafeRef()

THREADSAFE.

Member Function Documentation

◆ operator=() [1/4]

ThreadSafeRef& operator= ( const REFERENCE &  strongReference)

Adds a strong reference. THREADSAFE.

Parameters
[in]strongReferenceBaseRef with strong reference to an object.
Returns
*this.

◆ TryCompareAndSwap()

Bool TryCompareAndSwap ( const REFERENCE &  strongReference,
const REFERENCE &  compare 
)

Atomic compare and swap of the reference. THREADSAFE.

Parameters
[in]strongReferenceBaseRef with strong reference to an object.
[in]compareOld BaseRef to compare with current value.
Returns
True if the reference was exchanged.

◆ operator=() [2/4]

ThreadSafeRef& operator= ( const ThreadSafeRef< REFERENCE > &  src)

Copies a thread safe pointer.

Parameters
[in]srcSource thread safe pointer.
Returns
*this.

◆ operator=() [3/4]

ThreadSafeRef& operator= ( std::nullptr_t  )

Resets the thread safe pointer.

Returns
*this.

◆ operator=() [4/4]

ThreadSafeRef& operator= ( ThreadSafeRef< REFERENCE > &&  src)

Moves a thread safe pointer.

Parameters
[in]srcSource thread safe pointer.
Returns
*this.

◆ Get()

REFERENCE Get ( void  ) const

Constructs a new BaseRef with a strong reference. THREADSAFE.

Returns
BaseRef with a strong reference.

◆ operator REFERENCE()

operator REFERENCE ( ) const

Constructs a new BaseRef with a strong reference. THREADSAFE.

Returns
BaseRef with a strong reference.