Multithreaded Job System

You can download the code sample here : MultiThread

Languages

C++11

Description

This is a system that grabs multiple small independent jobs and executes them in different threads.

Advantages
  •  You can run from 1 to n amount of threads, n being dependent in the number of CPU cores that machine has. C++11 helps you to get that number by advising the number of concurrent threads in its function std::thread::hardware_concurrency(). If the value is not defined or not computable, it will return 0.
  • It’s fairly simple to use and to kept track of the jobs in the system at any given moment.
Disadvantages
  • Since the jobs are not executed immediately, the entity that assign that job may want to kept track of such job being completed or not.
  • In windows the system in charge of Peeking and Dispatching messages in the Operating system, needs to remain in the Main Thread.

Implementation

On initialization needs to open all the amount of threads needed to have the job system working properly.

In runtime it needs to kept track of all current working jobs in order to reassign priorities in the case that certain job has been to long pending to be executed.

And when the system shutdowns each worker thread needs to merge / join with the main thread in order to finish execution and clean up properly.

Features

This are a list of advisable features you want to implement in a Multithreaded Job System:

  • Being able to turn it on/off the system.
  • Have a priority system in place, so the more important jobs are executed as soon as possible,
  • Ability of Canceling a job, in the case that the job is not longer valid
  • Being able to detect the current state of the job we are waiting for.

Debugging features

On the exception that you are developing for a console, where the hardware requirements are the same across the machine, most likely that the hardware requirements differs from machine to machine. So one of the features you may want to implement in your multithreaded system is a way switch the amount of Worker Threads, in order to test the game performance on machine with lower capabilities than the one you are currently working at.

You can download the code sample here : MultiThread

jorge@tonojimenez.com (425)761-6488