This is a C++17 library for handling of Tasks / Jobs in a multi-threaded environment.
Examples
Minimal Example
#include <cassert>
#include <iostream>
static constexpr int k_DataSize = 100000;
static int s_ExampleData[k_DataSize] = {};
static void printFirst20Items()
{
for (int i = 0; i < 20; ++i)
{
std::printf("data[%i] = %i\n", i, s_ExampleData[i]);
}
}
int main()
{
for (int i = 0; i < k_DataSize; ++i)
{
s_ExampleData[i] = i;
}
std::printf("Before:\n");
printFirst20Items();
s_ExampleData,
k_DataSize,
Job::CountSplitter{6},
[](int* data, std::size_t data_size) {
for (std::size_t i = 0; i < data_size; ++i)
{
data[i] = data[i] * 5;
}
});
std::printf("After:\n");
printFirst20Items();
for (int i = 0; i < k_DataSize; ++i)
{
assert(s_ExampleData[i] == i * 5);
}
return 0;
}
API for a multi-threading job system.
void WaitOnTask(const Task *const task) noexcept
Waits until the specified task is done executing. This function will block but do work while being bl...
void Shutdown() noexcept
This will deallocate any memory used by the system and shutdown any threads created by 'bfJob::initia...
Task * ParallelFor(const std::size_t start, const std::size_t count, S &&splitter, F &&fn, Task *parent=nullptr)
Parallel for algorithm, splits the work up recursively splitting based on the splitter passed in.
void Initialize(const JobSystemMemoryRequirements &memory_requirements={}, void *const memory=nullptr) noexcept
Sets up the Job system and creates all the worker threads. The thread that calls 'Job::Initialize' is...
void TaskSubmit(Task *const self, const QueueType queue=QueueType::NORMAL) noexcept
Submits the task to the specified queue.
Architecture
Task
A Task is a single unit of work that can be scheduled by the Job System. Each Task has a total sizeof of 128bytes (2 * hardware interference size) with some of the bytes taken by essential bookkeeping date then the rest used for user storage.
A Tasks can be added as a child of another task, this means that when you wait on the parent Task then it will wait for all child Tasks as well.
Queues
A Queue hold a list of Tasks waiting to be executed. There are four different types of queues.
MAIN This queue has a guarantee that the task will be run on the main thread.
NORMAL Slightly lower priority than 'QueueType::HIGH'.
WORKER This queue has a guarantee that the task will never be run on the main thread.
Dependencies