Information on how to implement a job system using C++11

Asked 1 years ago, Updated 1 years ago, 117 views

Thank you for your help.


for use in games I don't think I can create a job system using std::thread.
Possible interfaces are:

//Generate std::thread by specifying the number of cores
int threadNum = 8;
JobManager.Create(threadNum);

// Runtime
int num = 10000;
// Execute (number of objects, function pointer, void* data).
JobManager.Start (num, pFunc, pData);
// stand by
JobManager.Join();

std::thread must pass a function pointer at the time of the constructor
Also, it costs a lot of money to generate threads.
I think it's difficult to do Start() and Join() every frame like a game.

Is it difficult to implement a job system with std::thread?
Thank you for your cooperation.

c++ c++11

2022-09-30 19:54

2 Answers

If you are concerned about the cost of creating/disappearing threads frequently, why not create a thread pool?

This is an image that creates several threads in advance and stores them in the pool, and then takes the threads out of the pool when needed to process them.The thread returns to the pool when it is finished and waits for the next action.

If you do not create a thread pool once at the start of the game program and recreate it until the end of the program, the cost of thread generation is only at the start of the game, and there is no further cost.

After a quick search, for example, the following page may be helpful:
https://cutlassfish.wordpress.com/2016/09/14/c-%E3%81%A7-worker-thread-%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3/


2022-09-30 19:54

The question is C++11, so it does not meet the requirements, but C++17 introduces parallel algorithm.

 std::vector<Data>data;
std::for_each(std::execution::par, begin(data), end(data), pFunc);

pFunc runs in parallel for each data.I think it depends on the implementation, but I expect thread pools to be used.


2022-09-30 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.