Skip to main content

JobSystem Class

A fixed-size thread pool for parallel job execution. More...

Declaration

class helios::engine::core::thread::JobSystem { ... }

Public Constructors Index

JobSystem (const std::size_t maxWorkerCount)

Constructs the job system and spawns worker threads. More...

Public Member Functions Index

template <typename Fn>
voidrunAndWait (std::size_t jobCount, Fn &&fn)

Enqueues jobCount jobs and blocks until all have finished. More...

Private Member Attributes Index

std::queue< std::function< void()> >jobQueue_
std::mutexmutex_
std::condition_variable_anyjobCondition_
std::condition_variabledoneCondition_
std::size_tmaxWorkerCount_

Maximum number of worker threads. More...

std::size_tpendingJobCount_ = 0

Number of jobs in the current batch that have not finished yet. More...

std::vector< std::jthread >workerThreads_

Description

A fixed-size thread pool for parallel job execution.

Maintains a pool of std::jthread workers that pull jobs from a shared queue. runAndWait() enqueues a batch of jobs and blocks until every job in that batch has completed.

Definition at line 38 of file JobSystem.ixx.

Public Constructors

JobSystem()

helios::engine::core::thread::JobSystem::JobSystem (const std::size_t maxWorkerCount)
inline explicit

Constructs the job system and spawns worker threads.

Parameters
maxWorkerCount

Number of worker threads to create. Typically std::thread::hardware_concurrency().

Definition at line 61 of file JobSystem.ixx.

61 explicit JobSystem(const std::size_t maxWorkerCount) : maxWorkerCount_(maxWorkerCount) {
62
63 workerThreads_.reserve(maxWorkerCount);
64
65 for (std::size_t i = 0; i < maxWorkerCount; ++i) {
66 workerThreads_.emplace_back([this](std::stop_token stopToken) {
67 while (!stopToken.stop_requested()) {
68 std::function<void()> job;
69
70 {
71 std::unique_lock<std::mutex> lock(mutex_);
72
73 if (!jobCondition_.wait(lock, stopToken, [&] {
74 return !jobQueue_.empty();
75 })) {
76 return;
77 }
78
79 job = std::move(jobQueue_.front());
80 jobQueue_.pop();
81 }
82
83 job();
84 }
85
86 });
87 }
88 }

Reference helios::engine::core::registerComponents.

Public Member Functions

runAndWait()

template <typename Fn>
void helios::engine::core::thread::JobSystem::runAndWait (std::size_t jobCount, Fn && fn)
inline

Enqueues jobCount jobs and blocks until all have finished.

Calls fn(i) for each index i in [0, jobCount) on a worker thread. Returns only after every invocation has completed.

Template Parameters
Fn

Callable type with signature void(std::size_t).

Parameters
jobCount

Number of parallel jobs to dispatch.

fn

Job body receiving the job index.

warning

Must not be called concurrently from multiple threads.

Definition at line 103 of file JobSystem.ixx.

103 void runAndWait(std::size_t jobCount, Fn&& fn) {
104 {
105 std::lock_guard lock(mutex_);
106
107 pendingJobCount_ = jobCount;
108
109 for (std::size_t i = 0; i < jobCount; ++i) {
110
111 jobQueue_.emplace([this, i, &fn] {
112 fn(i);
113
114 {
115 std::lock_guard lok(mutex_);
116 --pendingJobCount_;
117 }
118
119 doneCondition_.notify_one();
120 });
121
122 }
123 }
124
125 jobCondition_.notify_all();
126
127 std::unique_lock<std::mutex> lock(mutex_);
128
129 doneCondition_.wait(lock, [&] {
130 return pendingJobCount_ == 0;
131 });
132 }

Reference helios::engine::core::registerComponents.

Referenced by helios::engine::runtime::world::EntityMutationManager< TEntityManager >::flushParallel.

Private Member Attributes

doneCondition_

std::condition_variable helios::engine::core::thread::JobSystem::doneCondition_

Definition at line 43 of file JobSystem.ixx.

43 std::condition_variable doneCondition_;

jobCondition_

std::condition_variable_any helios::engine::core::thread::JobSystem::jobCondition_

Definition at line 42 of file JobSystem.ixx.

42 std::condition_variable_any jobCondition_;

jobQueue_

std::queue<std::function<void()> > helios::engine::core::thread::JobSystem::jobQueue_

Definition at line 40 of file JobSystem.ixx.

40 std::queue<std::function<void()>> jobQueue_;

maxWorkerCount_

std::size_t helios::engine::core::thread::JobSystem::maxWorkerCount_

Maximum number of worker threads.

Definition at line 46 of file JobSystem.ixx.

46 std::size_t maxWorkerCount_;

mutex_

std::mutex helios::engine::core::thread::JobSystem::mutex_

Definition at line 41 of file JobSystem.ixx.

41 std::mutex mutex_;

pendingJobCount_

std::size_t helios::engine::core::thread::JobSystem::pendingJobCount_ = 0

Number of jobs in the current batch that have not finished yet.

Definition at line 49 of file JobSystem.ixx.

49 std::size_t pendingJobCount_ = 0;

workerThreads_

std::vector<std::jthread> helios::engine::core::thread::JobSystem::workerThreads_

Definition at line 51 of file JobSystem.ixx.

51 std::vector<std::jthread> workerThreads_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.