Foundation
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
Foundation::Core::ThreadPool Class Reference

Atomic, lock-free Thread Pool implementation with fixed bounds. More...

#include <ThreadPool.hpp>

Public Member Functions

 ThreadPool (size_t numThreads, size_t maxTasks, Allocator *alloc, StringView name="ThreadPool")
 Construct a thread pool with the given number of worker threads.
 
template<typename T , typename... Args>
requires std::is_base_of_v<ThreadPoolJob, T>
TPushImpl (Args &&... args)
 Push a job implementing ThreadPoolJob to the thread pool.
 
template<typename Lambda , typename... Args>
auto Push (Lambda &&func, Args const &... args)
 Push a lambda job to the thread pool.
 
void Shutdown ()
 Shutdown the ThreadPool, potentially cancelling all pending jobs.
 
void Join ()
 Wait for all scheduled jobs to complete.
 
 ~ThreadPool ()
 Shutdown, without waiting for pending jobs.
 
size_t GetPendingJobCount () const noexcept
 
size_t GetCompletedJobCount () const noexcept
 
size_t GetTotalJobCount () const noexcept
 

Static Public Member Functions

static constexpr size_t getTaskSize (size_t size)
 

Private Member Functions

void ThreadPoolWorker (size_t id)
 

Private Attributes

AllocatormAllocator
 
String mName
 
Atomic< boolmShutdown {false}
 
Atomic< size_tmComplete {0}
 
Atomic< size_tmTotal {0}
 
JobQueue mJobs
 
Vector< ThreadmThreads
 

Detailed Description

Atomic, lock-free Thread Pool implementation with fixed bounds.

Constructor & Destructor Documentation

◆ ThreadPool()

Foundation::Core::ThreadPool::ThreadPool ( size_t  numThreads,
size_t  maxTasks,
Allocator alloc,
StringView  name = "ThreadPool" 
)

Construct a thread pool with the given number of worker threads.

Parameters
numThreadsNumber of worker threads to spawn.
maxTasksMax number of tasks that can be queued. Must be a power of two - see getTaskSize
allocAllocator to use for internal and job allocations
namePrefix for worker thread names ("name@id")

◆ ~ThreadPool()

Foundation::Core::ThreadPool::~ThreadPool ( )

Shutdown, without waiting for pending jobs.

Member Function Documentation

◆ GetCompletedJobCount()

size_t Foundation::Core::ThreadPool::GetCompletedJobCount ( ) const
inlinenoexcept

◆ GetPendingJobCount()

size_t Foundation::Core::ThreadPool::GetPendingJobCount ( ) const
inlinenoexcept

◆ getTaskSize()

static constexpr size_t Foundation::Core::ThreadPool::getTaskSize ( size_t  size)
inlinestaticconstexpr

Aligns a number to upper, closest power of 2 so that it's a valid maxTasks size.

◆ GetTotalJobCount()

size_t Foundation::Core::ThreadPool::GetTotalJobCount ( ) const
inlinenoexcept

◆ Join()

void Foundation::Core::ThreadPool::Join ( )

Wait for all scheduled jobs to complete.

Note
This MUST be called if you'd like all submitted work to complete before destruction.

◆ Push()

template<typename Lambda , typename... Args>
auto Foundation::Core::ThreadPool::Push ( Lambda &&  func,
Args const &...  args 
)
inline

Push a lambda job to the thread pool.

Returns
Future<func ReturnType> that will be set when the job is completed.

◆ PushImpl()

template<typename T , typename... Args>
requires std::is_base_of_v<ThreadPoolJob, T>
T * Foundation::Core::ThreadPool::PushImpl ( Args &&...  args)
inline

Push a job implementing ThreadPoolJob to the thread pool.

Note
This by itself does not return a future or any way to get the result of the job It's up to the implementation of the job to provide a way to get the result. See also ThreadPoolLambdaJob
Returns
Stable pointer of the pushed job. Lifetime guaranteed until the job's completion.

◆ Shutdown()

void Foundation::Core::ThreadPool::Shutdown ( )

Shutdown the ThreadPool, potentially cancelling all pending jobs.

Note
This does not cancel running jobs, but prevents any new jobs from being run/scheduled.

◆ ThreadPoolWorker()

void Foundation::Core::ThreadPool::ThreadPoolWorker ( size_t  id)
private

Member Data Documentation

◆ mAllocator

Allocator* Foundation::Core::ThreadPool::mAllocator
private

◆ mComplete

Atomic<size_t> Foundation::Core::ThreadPool::mComplete {0}
private

◆ mJobs

JobQueue Foundation::Core::ThreadPool::mJobs
private

◆ mName

String Foundation::Core::ThreadPool::mName
private

◆ mShutdown

Atomic<bool> Foundation::Core::ThreadPool::mShutdown {false}
private

◆ mThreads

Vector<Thread> Foundation::Core::ThreadPool::mThreads
private

◆ mTotal

Atomic<size_t> Foundation::Core::ThreadPool::mTotal {0}
private

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