Foundation
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Foundation::Async::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>
void PushImpl (Args &&... args)
 Push a job implementing ThreadPoolJob to the thread pool.
 
template<typename Lambda , typename... Args>
auto Push (Lambda &&func, Args &&... 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 ()
 

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
 
JobQueue::Writer mJobsWriter
 
Vector< ThreadmThreads
 

Detailed Description

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

Constructor & Destructor Documentation

◆ ThreadPool()

Foundation::Async::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.
allocAllocator to use for internal and job allocations
namePrefix for worker thread names ("name@id")

◆ ~ThreadPool()

Foundation::Async::ThreadPool::~ThreadPool ( )

Shutdown, without waiting for pending jobs.

Member Function Documentation

◆ Join()

void Foundation::Async::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::Async::ThreadPool::Push ( Lambda &&  func,
Args &&...  args 
)
inline

Push a lambda job to the thread pool.

Returns
SharedPromise<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>
void Foundation::Async::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

◆ Shutdown()

void Foundation::Async::ThreadPool::Shutdown ( )

Shutdown the ThreadPool, potentially cancelling all pending jobs.

Note
This does not cancel running jobs.

◆ ThreadPoolWorker()

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

Member Data Documentation

◆ mAllocator

Allocator* Foundation::Async::ThreadPool::mAllocator
private

◆ mComplete

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

◆ mJobs

JobQueue Foundation::Async::ThreadPool::mJobs
private

◆ mJobsWriter

JobQueue::Writer Foundation::Async::ThreadPool::mJobsWriter
private

◆ mName

String Foundation::Async::ThreadPool::mName
private

◆ mShutdown

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

◆ mThreads

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

◆ mTotal

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

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