ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::WorkStealingDeque< T > Class Template Reference

Work-stealing deque for per-thread task queues in a thread pool. More...

#include <thread_pool.hpp>

Public Member Functions

 WorkStealingDeque (size_t capacity=DEFAULT_CAPACITY)
auto push (T &&item) -> bool
auto push (T const &item) -> bool
auto pop (T &item) -> bool
auto steal (T &item) -> bool
auto size () const -> size_t
auto empty () const -> bool

Static Public Attributes

static constexpr size_t CACHE_LINE_SIZE = 64
static constexpr size_t DEFAULT_CAPACITY = 1024

Detailed Description

template<typename T>
class threadschedule::WorkStealingDeque< T >

Work-stealing deque for per-thread task queues in a thread pool.

Implements a double-ended queue where the owning worker thread pushes and pops tasks from the top, while other ("thief") threads steal tasks from the bottom. This asymmetry reduces contention under typical workloads because the owner operates on one end and thieves on the other.

Thread safety
All public operations are serialized by an internal mutex, so the deque is safe to use concurrently from any number of threads. The atomic counters (top_ / bottom_) exist for a fast, lock-free size() / empty() snapshot but do not make push/pop/steal lock-free; the mutex is always acquired.
Capacity
The deque has a fixed capacity set at construction (default DEFAULT_CAPACITY = 1024). push() returns false when the deque is full; it never reallocates. Choose a capacity large enough for your expected burst size or use an overflow queue externally (as HighPerformancePool does).
Memory layout
Each stored item is wrapped in an AlignedItem that is aligned to CACHE_LINE_SIZE (64 bytes) to prevent false sharing between adjacent elements when multiple threads access neighboring slots.
Copyability / movability
Not copyable and not movable (contains a std::mutex).
Template Parameters
TThe task type. Must be move-constructible.

Definition at line 50 of file thread_pool.hpp.

Constructor & Destructor Documentation

◆ WorkStealingDeque()

template<typename T>
threadschedule::WorkStealingDeque< T >::WorkStealingDeque ( size_t capacity = DEFAULT_CAPACITY)
inlineexplicit

Definition at line 77 of file thread_pool.hpp.

Member Function Documentation

◆ empty()

template<typename T>
auto threadschedule::WorkStealingDeque< T >::empty ( ) const -> bool
inlinenodiscard

Definition at line 156 of file thread_pool.hpp.

◆ pop()

template<typename T>
auto threadschedule::WorkStealingDeque< T >::pop ( T & item) -> bool
inlinenodiscard

Definition at line 115 of file thread_pool.hpp.

◆ push() [1/2]

template<typename T>
auto threadschedule::WorkStealingDeque< T >::push ( T && item) -> bool
inlinenodiscard

Definition at line 83 of file thread_pool.hpp.

◆ push() [2/2]

template<typename T>
auto threadschedule::WorkStealingDeque< T >::push ( T const & item) -> bool
inlinenodiscard

Definition at line 99 of file thread_pool.hpp.

◆ size()

template<typename T>
auto threadschedule::WorkStealingDeque< T >::size ( ) const -> size_t
inlinenodiscard

Definition at line 149 of file thread_pool.hpp.

◆ steal()

template<typename T>
auto threadschedule::WorkStealingDeque< T >::steal ( T & item) -> bool
inlinenodiscard

Definition at line 133 of file thread_pool.hpp.

Member Data Documentation

◆ CACHE_LINE_SIZE

template<typename T>
size_t threadschedule::WorkStealingDeque< T >::CACHE_LINE_SIZE = 64
staticconstexpr

Definition at line 53 of file thread_pool.hpp.

◆ DEFAULT_CAPACITY

template<typename T>
size_t threadschedule::WorkStealingDeque< T >::DEFAULT_CAPACITY = 1024
staticconstexpr

Definition at line 54 of file thread_pool.hpp.


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