Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::atomic_shared_ptr< T > Class Template Reference

#include <atomic_shared_ptr.h>

Collaboration diagram for kcenon::thread::atomic_shared_ptr< T >:
Collaboration graph

Public Member Functions

 atomic_shared_ptr () noexcept=default
 Default constructor - initializes with nullptr.
 
 atomic_shared_ptr (std::shared_ptr< T > ptr) noexcept
 Construct with initial shared_ptr.
 
 atomic_shared_ptr (const atomic_shared_ptr &other)
 Copy constructor - atomically copies the shared_ptr.
 
atomic_shared_ptroperator= (const atomic_shared_ptr &other)
 Copy assignment - atomically copies the shared_ptr.
 
 atomic_shared_ptr (atomic_shared_ptr &&other) noexcept
 Move constructor.
 
atomic_shared_ptroperator= (atomic_shared_ptr &&other) noexcept
 Move assignment.
 
atomic_shared_ptroperator= (std::shared_ptr< T > ptr) noexcept
 Assign from shared_ptr.
 
void store (std::shared_ptr< T > ptr, std::memory_order order=std::memory_order_seq_cst) noexcept
 Atomically store a new value.
 
std::shared_ptr< T > load (std::memory_order order=std::memory_order_seq_cst) const noexcept
 Atomically load the current value.
 
std::shared_ptr< T > exchange (std::shared_ptr< T > ptr, std::memory_order order=std::memory_order_seq_cst) noexcept
 Atomically exchange the value.
 
bool compare_exchange_weak (std::shared_ptr< T > &expected, std::shared_ptr< T > desired, std::memory_order success=std::memory_order_seq_cst, std::memory_order failure=std::memory_order_seq_cst) noexcept
 Atomically compare and exchange (weak)
 
bool compare_exchange_strong (std::shared_ptr< T > &expected, std::shared_ptr< T > desired, std::memory_order success=std::memory_order_seq_cst, std::memory_order failure=std::memory_order_seq_cst) noexcept
 Atomically compare and exchange (strong)
 
 operator std::shared_ptr< T > () const noexcept
 Conversion operator to shared_ptr.
 
T & operator* () const noexcept
 Dereference operator.
 
T * operator-> () const noexcept
 Member access operator.
 
 operator bool () const noexcept
 Boolean conversion - check if not null.
 
T * get_unsafe () const noexcept
 Get raw pointer (unsafe - for debugging only)
 
void reset () noexcept
 Reset to nullptr.
 
void reset (T *ptr) noexcept
 Reset to new value.
 

Private Attributes

std::shared_ptr< T > ptr_ {nullptr}
 

Detailed Description

template<typename T>
class kcenon::thread::atomic_shared_ptr< T >

Definition at line 50 of file atomic_shared_ptr.h.

Constructor & Destructor Documentation

◆ atomic_shared_ptr() [1/4]

template<typename T >
kcenon::thread::atomic_shared_ptr< T >::atomic_shared_ptr ( )
defaultnoexcept

◆ atomic_shared_ptr() [2/4]

template<typename T >
kcenon::thread::atomic_shared_ptr< T >::atomic_shared_ptr ( std::shared_ptr< T > ptr)
inlineexplicitnoexcept

Construct with initial shared_ptr.

Parameters
ptrInitial value

Definition at line 61 of file atomic_shared_ptr.h.

62 : ptr_(std::move(ptr)) {}

◆ atomic_shared_ptr() [3/4]

template<typename T >
kcenon::thread::atomic_shared_ptr< T >::atomic_shared_ptr ( const atomic_shared_ptr< T > & other)
inline

Copy constructor - atomically copies the shared_ptr.

Parameters
otherSource atomic_shared_ptr

Definition at line 68 of file atomic_shared_ptr.h.

69 : ptr_(other.load()) {}

◆ atomic_shared_ptr() [4/4]

template<typename T >
kcenon::thread::atomic_shared_ptr< T >::atomic_shared_ptr ( atomic_shared_ptr< T > && other)
inlinenoexcept

Move constructor.

Parameters
otherSource atomic_shared_ptr

Definition at line 87 of file atomic_shared_ptr.h.

88 : ptr_(other.exchange(nullptr)) {}

Member Function Documentation

◆ compare_exchange_strong()

template<typename T >
bool kcenon::thread::atomic_shared_ptr< T >::compare_exchange_strong ( std::shared_ptr< T > & expected,
std::shared_ptr< T > desired,
std::memory_order success = std::memory_order_seq_cst,
std::memory_order failure = std::memory_order_seq_cst )
inlinenoexcept

Atomically compare and exchange (strong)

Parameters
expectedExpected value (updated on failure)
desiredNew value if comparison succeeds
successMemory ordering on success
failureMemory ordering on failure
Returns
true if exchange succeeded

Only fails if comparison actually fails (no spurious failures).

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 195 of file atomic_shared_ptr.h.

199 {
200#if __cpp_lib_atomic_shared_ptr >= 201711L
201 return ptr_.compare_exchange_strong(expected, std::move(desired), success, failure);
202#else
203 return std::atomic_compare_exchange_strong_explicit(
204 &ptr_, &expected, std::move(desired), success, failure);
205#endif
206 }

References kcenon::thread::atomic_shared_ptr< T >::ptr_, and kcenon::thread::success.

◆ compare_exchange_weak()

template<typename T >
bool kcenon::thread::atomic_shared_ptr< T >::compare_exchange_weak ( std::shared_ptr< T > & expected,
std::shared_ptr< T > desired,
std::memory_order success = std::memory_order_seq_cst,
std::memory_order failure = std::memory_order_seq_cst )
inlinenoexcept

Atomically compare and exchange (weak)

Parameters
expectedExpected value (updated on failure)
desiredNew value if comparison succeeds
successMemory ordering on success
failureMemory ordering on failure
Returns
true if exchange succeeded

May spuriously fail even if comparison would succeed. Use in loops for lock-free algorithms.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 172 of file atomic_shared_ptr.h.

176 {
177#if __cpp_lib_atomic_shared_ptr >= 201711L
178 return ptr_.compare_exchange_weak(expected, std::move(desired), success, failure);
179#else
180 return std::atomic_compare_exchange_weak_explicit(
181 &ptr_, &expected, std::move(desired), success, failure);
182#endif
183 }

References kcenon::thread::atomic_shared_ptr< T >::ptr_, and kcenon::thread::success.

◆ exchange()

template<typename T >
std::shared_ptr< T > kcenon::thread::atomic_shared_ptr< T >::exchange ( std::shared_ptr< T > ptr,
std::memory_order order = std::memory_order_seq_cst )
inlinenodiscardnoexcept

Atomically exchange the value.

Parameters
ptrNew value
orderMemory ordering
Returns
Previous value
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 151 of file atomic_shared_ptr.h.

153 {
154#if __cpp_lib_atomic_shared_ptr >= 201711L
155 return ptr_.exchange(std::move(ptr), order);
156#else
157 return std::atomic_exchange_explicit(&ptr_, std::move(ptr), order);
158#endif
159 }

References kcenon::thread::atomic_shared_ptr< T >::ptr_.

◆ get_unsafe()

template<typename T >
T * kcenon::thread::atomic_shared_ptr< T >::get_unsafe ( ) const
inlinenodiscardnoexcept

Get raw pointer (unsafe - for debugging only)

Returns
Raw pointer, may become dangling
Warning
The returned pointer is not protected. Only use for debugging or when you guarantee lifetime externally.
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 247 of file atomic_shared_ptr.h.

247 {
248 return load().get();
249 }
std::shared_ptr< T > load(std::memory_order order=std::memory_order_seq_cst) const noexcept
Atomically load the current value.

References kcenon::thread::atomic_shared_ptr< T >::load().

Here is the call graph for this function:

◆ load()

template<typename T >
std::shared_ptr< T > kcenon::thread::atomic_shared_ptr< T >::load ( std::memory_order order = std::memory_order_seq_cst) const
inlinenodiscardnoexcept

Atomically load the current value.

Parameters
orderMemory ordering
Returns
Current shared_ptr value

The returned shared_ptr keeps the object alive as long as it exists - no manual hazard pointer management needed.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 136 of file atomic_shared_ptr.h.

137 {
138#if __cpp_lib_atomic_shared_ptr >= 201711L
139 return ptr_.load(order);
140#else
141 return std::atomic_load_explicit(&ptr_, order);
142#endif
143 }

References kcenon::thread::atomic_shared_ptr< T >::ptr_.

Referenced by kcenon::thread::atomic_shared_ptr< T >::get_unsafe(), kcenon::thread::atomic_shared_ptr< T >::operator bool(), kcenon::thread::atomic_shared_ptr< T >::operator std::shared_ptr< T >(), kcenon::thread::atomic_shared_ptr< T >::operator*(), kcenon::thread::atomic_shared_ptr< T >::operator->(), and kcenon::thread::atomic_shared_ptr< T >::operator=().

Here is the caller graph for this function:

◆ operator bool()

template<typename T >
kcenon::thread::atomic_shared_ptr< T >::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Boolean conversion - check if not null.

Definition at line 237 of file atomic_shared_ptr.h.

237 {
238 return load() != nullptr;
239 }

References kcenon::thread::atomic_shared_ptr< T >::load().

Here is the call graph for this function:

◆ operator std::shared_ptr< T >()

template<typename T >
kcenon::thread::atomic_shared_ptr< T >::operator std::shared_ptr< T > ( ) const
inlinenodiscardnoexcept

Conversion operator to shared_ptr.

Returns
Current value (atomically loaded)

Definition at line 212 of file atomic_shared_ptr.h.

212 {
213 return load();
214 }

References kcenon::thread::atomic_shared_ptr< T >::load().

Here is the call graph for this function:

◆ operator*()

template<typename T >
T & kcenon::thread::atomic_shared_ptr< T >::operator* ( ) const
inlinenodiscardnoexcept

Dereference operator.

Returns
Reference to managed object
Warning
Undefined behavior if pointer is null
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 221 of file atomic_shared_ptr.h.

221 {
222 return *load();
223 }

References kcenon::thread::atomic_shared_ptr< T >::load().

Here is the call graph for this function:

◆ operator->()

template<typename T >
T * kcenon::thread::atomic_shared_ptr< T >::operator-> ( ) const
inlinenodiscardnoexcept

Member access operator.

Returns
Pointer to managed object
Warning
Undefined behavior if pointer is null
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 230 of file atomic_shared_ptr.h.

230 {
231 return load().get();
232 }

References kcenon::thread::atomic_shared_ptr< T >::load().

Here is the call graph for this function:

◆ operator=() [1/3]

template<typename T >
atomic_shared_ptr & kcenon::thread::atomic_shared_ptr< T >::operator= ( atomic_shared_ptr< T > && other)
inlinenoexcept

Move assignment.

Parameters
otherSource atomic_shared_ptr
Returns
Reference to this

Definition at line 95 of file atomic_shared_ptr.h.

95 {
96 if (this != &other) {
97 store(other.exchange(nullptr));
98 }
99 return *this;
100 }
void store(std::shared_ptr< T > ptr, std::memory_order order=std::memory_order_seq_cst) noexcept
Atomically store a new value.

References kcenon::thread::atomic_shared_ptr< T >::store().

Here is the call graph for this function:

◆ operator=() [2/3]

template<typename T >
atomic_shared_ptr & kcenon::thread::atomic_shared_ptr< T >::operator= ( const atomic_shared_ptr< T > & other)
inline

Copy assignment - atomically copies the shared_ptr.

Parameters
otherSource atomic_shared_ptr
Returns
Reference to this
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 76 of file atomic_shared_ptr.h.

76 {
77 if (this != &other) {
78 store(other.load());
79 }
80 return *this;
81 }

References kcenon::thread::atomic_shared_ptr< T >::load(), and kcenon::thread::atomic_shared_ptr< T >::store().

Here is the call graph for this function:

◆ operator=() [3/3]

template<typename T >
atomic_shared_ptr & kcenon::thread::atomic_shared_ptr< T >::operator= ( std::shared_ptr< T > ptr)
inlinenoexcept

Assign from shared_ptr.

Parameters
ptrNew value
Returns
Reference to this

Definition at line 107 of file atomic_shared_ptr.h.

107 {
108 store(std::move(ptr));
109 return *this;
110 }

References kcenon::thread::atomic_shared_ptr< T >::store().

Here is the call graph for this function:

◆ reset() [1/2]

template<typename T >
void kcenon::thread::atomic_shared_ptr< T >::reset ( )
inlinenoexcept

Reset to nullptr.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 254 of file atomic_shared_ptr.h.

254 {
255 store(nullptr);
256 }

References kcenon::thread::atomic_shared_ptr< T >::store().

Here is the call graph for this function:

◆ reset() [2/2]

template<typename T >
void kcenon::thread::atomic_shared_ptr< T >::reset ( T * ptr)
inlinenoexcept

Reset to new value.

Parameters
ptrNew managed object

Definition at line 262 of file atomic_shared_ptr.h.

262 {
263 store(std::shared_ptr<T>(ptr));
264 }

References kcenon::thread::atomic_shared_ptr< T >::store().

Here is the call graph for this function:

◆ store()

template<typename T >
void kcenon::thread::atomic_shared_ptr< T >::store ( std::shared_ptr< T > ptr,
std::memory_order order = std::memory_order_seq_cst )
inlinenoexcept

Atomically store a new value.

Parameters
ptrNew value
orderMemory ordering

Uses memory_order_seq_cst by default for maximum safety.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/atomic_shared_ptr.h.

Definition at line 119 of file atomic_shared_ptr.h.

120 {
121#if __cpp_lib_atomic_shared_ptr >= 201711L
122 ptr_.store(std::move(ptr), order);
123#else
124 std::atomic_store_explicit(&ptr_, std::move(ptr), order);
125#endif
126 }

References kcenon::thread::atomic_shared_ptr< T >::ptr_.

Referenced by kcenon::thread::atomic_shared_ptr< T >::operator=(), kcenon::thread::atomic_shared_ptr< T >::operator=(), kcenon::thread::atomic_shared_ptr< T >::operator=(), kcenon::thread::atomic_shared_ptr< T >::reset(), and kcenon::thread::atomic_shared_ptr< T >::reset().

Here is the caller graph for this function:

Member Data Documentation

◆ ptr_


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