heitaoflower / BF-Job-System

A C++17 job system library for use in game engines.

Home Page:https://blufedora.github.io/BF-Job-System/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BluFedora Job System Library

This is a C++17 library for handling of Tasks / Jobs in a multi-threaded environment.

Examples

Minimal Example

#include "bf/JobSystemExt.hpp"

#include <cassert>
#include <iostream>

static constexpr int k_DataSize = 100000;

static int s_ExampleData[k_DataSize] = {};

// Print the first 20 items for brevity
static void printFirst20Items()
{
  for (int i = 0; i < 20; ++i)
  {
    std::printf("data[%i] = %i\n", i, s_ExampleData[i]);
  }
}

int main()
{
  if (!bf::job::initialize())
  {
    return 1;
  }

  // Initialize Dummy Data
  for (int i = 0; i < k_DataSize; ++i)
  {
    s_ExampleData[i] = i;
  }

  std::printf("Before:\n");
  printFirst20Items();

  auto* t = bf::job::parallel_for(
   s_ExampleData, 
   k_DataSize, 
   bf::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;
     }
   });

  bf::job::taskSubmit(t);

  bf::job::waitOnTask(t);

  std::printf("After:\n");
  printFirst20Items();

  // Check that the jobs finished working on all items.
  for (int i = 0; i < k_DataSize; ++i)
  {
    assert(s_ExampleData[i] == i * 5);
  }

  bf::job::shutdown();

  return 0;
}

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 Task 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'.
  • BACKGROUND This queue has a guarantee that the task will never be run on the main thread.

Dependencies

  • C++17 or higher

Libraries Used

About

A C++17 job system library for use in game engines.

https://blufedora.github.io/BF-Job-System/

License:MIT License


Languages

Language:C++ 75.1%Language:C 21.3%Language:CMake 2.4%Language:Makefile 1.2%