It's time to implement a threadpool in my game engine with std::mutex, std::condition_variable, std::unique_lock and std::lock_guard. I have read a lot about Lock-free Programming, but that seems really hard to get it right. The number of jobs I need to execute each frame is approx. 40-200. So, I have couple of questions that needs to be answered before I do any attempts to implement this stuff.
How many threads should the threadpool contain? As many as the number of cores on the CPU? And if this is the case, should I create nrThreadsThreadPool = nrCpuCores - 1(main thread)?
Example:
- Current computer: 4 cores, 4 threads.
- Threadpool contains 3 threads.
Let's say that I have 17500 objects in an array and I need to perform frustum culling on each object. Should I send 5000, 5000, 5000 as three jobs to the threadpool and perform 2500 in the main thread? This solution seems to be non-optimal, because each frustum culling test will take different amount of time to complete, and this can result in cores that has nothing to do. Or, should I create a work size variable with a size of 512 or something similar, and calculate the number of jobs that I need to send to the threadpool?
threadpool.push(job_0, arrayStart_0, arrayEnd_0) //512 frustum culling.
threadpool.push(job_1, arrayStart_1, arrayEnd_1) //512 frustum culling.
threadpool.push(job_2, arrayStart_2, arrayEnd_2) //512 frustum culling.
threadpool.push(job_3, arrayStart_3, arrayEnd_3) //512 frustum culling.
...
threadpool.push(job_n-1, arrayStart_n-1, arrayEnd_n-1) //512 frustum culling.
mainThreadFrustumCulling() //The main thread should execute the remaining frustum culling tests.
threadpool.wait() //Wait for frustum culling to finish in the thread pool.
doSomethingWithTheResults()
Aucun commentaire:
Enregistrer un commentaire