group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
WorkerPool Class Reference

#include <WorkerPool.hpp>

Public Member Functions

 WorkerPool (int numWorkers)
 Spin up numWorkers persistent threads.
 ~WorkerPool ()
 WorkerPool (const WorkerPool &)=delete
WorkerPooloperator= (const WorkerPool &)=delete
 WorkerPool (WorkerPool &&)=delete
WorkerPooloperator= (WorkerPool &&)=delete
int numWorkers () const noexcept
 Number of worker threads (excluding the calling thread).
void parallelFor (int count, const std::function< void(int begin, int end)> &fn)
 Run fn in parallel over the index range [0, count).

Private Member Functions

void workerLoop (int workerId)

Private Attributes

std::vector< std::thread > workers_
std::mutex mtx_
std::condition_variable cvWork_
std::condition_variable cvDone_
const std::function< void(int, int)> * currentFn_ = nullptr
int currentTotal_ = 0
int currentChunkSize_ = 0
int currentGeneration_ = 0
std::atomic< int > outstanding_ {0}
std::atomic< bool > stop_ {false}

Constructor & Destructor Documentation

◆ WorkerPool() [1/3]

WorkerPool::WorkerPool ( int numWorkers)
explicit

Spin up numWorkers persistent threads.

Parameters
numWorkersWorker count. Caller picks; a sane default is std::thread::hardware_concurrency() / 2 so we leave half the cores for the rest of the system (server + bots in our test scenario, OS in prod).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~WorkerPool()

WorkerPool::~WorkerPool ( )

◆ WorkerPool() [2/3]

WorkerPool::WorkerPool ( const WorkerPool & )
delete
Here is the call graph for this function:

◆ WorkerPool() [3/3]

WorkerPool::WorkerPool ( WorkerPool && )
delete
Here is the call graph for this function:

Member Function Documentation

◆ numWorkers()

int WorkerPool::numWorkers ( ) const
inlinenodiscardnoexcept

Number of worker threads (excluding the calling thread).

Here is the caller graph for this function:

◆ operator=() [1/2]

WorkerPool & WorkerPool::operator= ( const WorkerPool & )
delete
Here is the call graph for this function:

◆ operator=() [2/2]

WorkerPool & WorkerPool::operator= ( WorkerPool && )
delete
Here is the call graph for this function:

◆ parallelFor()

void WorkerPool::parallelFor ( int count,
const std::function< void(int begin, int end)> & fn )

Run fn in parallel over the index range [0, count).

The range is split into numWorkers + 1 chunks (workers + the calling thread), each worker receives one chunk, and the calling thread runs the last chunk inline before waiting for the others. Blocks until every chunk has returned.

fn(begin, end) is called once per chunk with a half-open range. Different threads may be running fn concurrently — fn must be thread-safe with respect to itself, and must not mutate shared state across chunks without explicit synchronization.

◆ workerLoop()

void WorkerPool::workerLoop ( int workerId)
private

Member Data Documentation

◆ currentChunkSize_

int WorkerPool::currentChunkSize_ = 0
private

◆ currentFn_

const std::function<void(int, int)>* WorkerPool::currentFn_ = nullptr
private

◆ currentGeneration_

int WorkerPool::currentGeneration_ = 0
private

◆ currentTotal_

int WorkerPool::currentTotal_ = 0
private

◆ cvDone_

std::condition_variable WorkerPool::cvDone_
private

◆ cvWork_

std::condition_variable WorkerPool::cvWork_
private

◆ mtx_

std::mutex WorkerPool::mtx_
private

◆ outstanding_

std::atomic<int> WorkerPool::outstanding_ {0}
private

◆ stop_

std::atomic<bool> WorkerPool::stop_ {false}
private

◆ workers_

std::vector<std::thread> WorkerPool::workers_
private

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