|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Lock-free work-stealing deque based on Chase-Lev algorithm. More...
#include <work_stealing_deque.h>


Public Member Functions | |
| work_stealing_deque (std::size_t log_initial_size=LOG_INITIAL_SIZE) | |
| Constructs an empty work-stealing deque. | |
| ~work_stealing_deque () | |
| Destructor - cleans up the circular array. | |
| work_stealing_deque (const work_stealing_deque &)=delete | |
| work_stealing_deque & | operator= (const work_stealing_deque &)=delete |
| work_stealing_deque (work_stealing_deque &&)=delete | |
| work_stealing_deque & | operator= (work_stealing_deque &&)=delete |
| void | push (T item) |
| Push an element onto the bottom of the deque (owner only) | |
| std::optional< T > | pop () |
| Pop an element from the bottom of the deque (owner only) | |
| std::optional< T > | steal () |
| Steal an element from the top of the deque (thief threads) | |
| std::vector< T > | steal_batch (std::size_t max_count) |
| Steal multiple elements from the top of the deque (thief threads) | |
| bool | empty () const noexcept |
| Check if the deque appears empty. | |
| std::size_t | size () const noexcept |
| Get approximate size of the deque. | |
| std::size_t | capacity () const noexcept |
| Get the capacity of the current array. | |
| void | cleanup_old_arrays () |
| Clear all old arrays (for memory cleanup) | |
Static Public Attributes | |
| static constexpr std::size_t | LOG_INITIAL_SIZE = 5 |
| Default initial log capacity (2^LOG_INITIAL_SIZE = 32 elements) | |
Private Attributes | |
| std::atomic< std::int64_t > | top_ |
| std::atomic< std::int64_t > | bottom_ |
| std::atomic< circular_array< T > * > | array_ |
| std::vector< circular_array< T > * > | old_arrays_ |
Static Private Attributes | |
| static constexpr std::size_t | CACHE_LINE_SIZE = 64 |
Lock-free work-stealing deque based on Chase-Lev algorithm.
This class implements a work-stealing deque (double-ended queue) using the Chase-Lev algorithm. It provides efficient local operations for the owner thread (push/pop) and concurrent stealing for other threads.
Key Features:
Algorithm Reference: "Dynamic Circular Work-Stealing Deque" (Chase & Lev, 2005)
Memory Layout:
Thread Safety:
| T | Element type (must be pointer or trivially copyable) |
Definition at line 36 of file numa_work_stealer.h.
|
inlineexplicit |
Constructs an empty work-stealing deque.
| log_initial_size | Initial capacity as log base 2 (default: 5, i.e., 32 elements) |
Definition at line 156 of file work_stealing_deque.h.
|
inline |
Destructor - cleans up the circular array.
Definition at line 165 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::array_.
|
delete |
|
delete |
|
inlinenodiscardnoexcept |
Get the capacity of the current array.
Definition at line 375 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::array_.
|
inline |
Clear all old arrays (for memory cleanup)
Definition at line 385 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::old_arrays_.
|
inlinenodiscardnoexcept |
Check if the deque appears empty.
Definition at line 352 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::bottom_, and kcenon::thread::lockfree::work_stealing_deque< T >::top_.
|
delete |
|
delete |
|
inlinenodiscard |
Pop an element from the bottom of the deque (owner only)
Time Complexity: O(1)
Definition at line 214 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::array_, kcenon::thread::lockfree::work_stealing_deque< T >::bottom_, kcenon::thread::lockfree::circular_array< T >::get(), and kcenon::thread::lockfree::work_stealing_deque< T >::top_.

|
inline |
Push an element onto the bottom of the deque (owner only)
| item | Element to push |
Time Complexity: O(1) amortized (O(n) when resizing)
Definition at line 184 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::array_, kcenon::thread::lockfree::work_stealing_deque< T >::bottom_, kcenon::thread::lockfree::circular_array< T >::grow(), kcenon::thread::lockfree::work_stealing_deque< T >::old_arrays_, kcenon::thread::lockfree::circular_array< T >::put(), kcenon::thread::lockfree::circular_array< T >::size(), and kcenon::thread::lockfree::work_stealing_deque< T >::top_.

|
inlinenodiscardnoexcept |
Get approximate size of the deque.
Definition at line 364 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::bottom_, and kcenon::thread::lockfree::work_stealing_deque< T >::top_.
|
inlinenodiscard |
Steal an element from the top of the deque (thief threads)
Time Complexity: O(1)
Definition at line 253 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::array_, kcenon::thread::lockfree::work_stealing_deque< T >::bottom_, kcenon::thread::lockfree::circular_array< T >::get(), and kcenon::thread::lockfree::work_stealing_deque< T >::top_.

|
inlinenodiscard |
Steal multiple elements from the top of the deque (thief threads)
| max_count | Maximum number of elements to steal |
Time Complexity: O(max_count)
This method attempts to steal up to max_count elements atomically. The batch steal uses a CAS loop to claim a range of elements from the top.
Key behaviors:
Memory Ordering:
Definition at line 299 of file work_stealing_deque.h.
References kcenon::thread::lockfree::work_stealing_deque< T >::array_, kcenon::thread::lockfree::work_stealing_deque< T >::bottom_, kcenon::thread::lockfree::circular_array< T >::get(), and kcenon::thread::lockfree::work_stealing_deque< T >::top_.

|
private |
Definition at line 398 of file work_stealing_deque.h.
Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::capacity(), kcenon::thread::lockfree::work_stealing_deque< T >::pop(), kcenon::thread::lockfree::work_stealing_deque< T >::push(), kcenon::thread::lockfree::work_stealing_deque< T >::steal(), kcenon::thread::lockfree::work_stealing_deque< T >::steal_batch(), and kcenon::thread::lockfree::work_stealing_deque< T >::~work_stealing_deque().
|
private |
Definition at line 397 of file work_stealing_deque.h.
Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::empty(), kcenon::thread::lockfree::work_stealing_deque< T >::pop(), kcenon::thread::lockfree::work_stealing_deque< T >::push(), kcenon::thread::lockfree::work_stealing_deque< T >::size(), kcenon::thread::lockfree::work_stealing_deque< T >::steal(), and kcenon::thread::lockfree::work_stealing_deque< T >::steal_batch().
|
staticconstexprprivate |
Definition at line 394 of file work_stealing_deque.h.
|
staticconstexpr |
Default initial log capacity (2^LOG_INITIAL_SIZE = 32 elements)
Definition at line 150 of file work_stealing_deque.h.
|
private |
Definition at line 401 of file work_stealing_deque.h.
Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::cleanup_old_arrays(), and kcenon::thread::lockfree::work_stealing_deque< T >::push().
|
private |
Definition at line 396 of file work_stealing_deque.h.
Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::empty(), kcenon::thread::lockfree::work_stealing_deque< T >::pop(), kcenon::thread::lockfree::work_stealing_deque< T >::push(), kcenon::thread::lockfree::work_stealing_deque< T >::size(), kcenon::thread::lockfree::work_stealing_deque< T >::steal(), and kcenon::thread::lockfree::work_stealing_deque< T >::steal_batch().