JobManager - mjfalkenstein/GameProject GitHub Wiki

The JobManager module allows for easily executing parallel code and synchronizing multiple jobs. When the engine is initialized, threads are spawned so that there is a worker per logical core, excluding one which is saved for the main thread. These threads will sleep until jobs are added to the JobManager, at which point they will process jobs until there are none remaining, then sleep again until more added. There are also utility functions to check whether all jobs have been completed.

class JobManager::Job
Interface for a job that can be executed in parallel, may be changed to use function pointers instead. Any class that inherits from this class can be passed to the JobManager for execution.

void JobManager::addJob(Job*)
Adds a job to the queue, waking the worker threads if they are sleeping. The function will return immediately, and the job will be executed on a separate thread as soon as a worker is available. The pointer currently must remain valid until after it is completed, so be careful if adding a pointer to a stack object if it may go out of scope before it is executed. If function pointers are used, this hazard will be mitigated.

void JobManager::wait()
Waits for all submitted jobs to complete. Will also process queued jobs on the calling thread to improve job throughput.

bool JobManager::hasPending()
Returns whether there are jobs that have not yet been completed by the worker threads. Can be used to wait for jobs, but unlike the dedicated wait() function, allows the calling thread to do other work in the meantime.