Creating threads has a cost. Why pay it when you don't have to?Do you think it's better to run jobs on the main thread or instead spawn an extra worker thread and let the main thread sleep while there are jobs still pending?
If your jobs are the kind of "update culling", "animate n entities", definitely run them also in the main thread, especially if your frame processing cannot proceed further without completing them first.
I'd have suggest having the number of worker threads be equal to the number of cores or maybe even double that. (Forum member Frob has said in his experience 2 times the number of virtual cores is usually a good way to go).
I'm designing an engine for my big 3D game project, and I want to make sure everything is very scalable to processors with many cores, without having more threads than necessary spawned at a time.
This is the architecture I'm considering right now in terms of different threads:
While this makes sense to me for processors like Intel i7's or AMD FX's which generally have more than 4 (logical) cores, for a 4-core processor like an i5, there is only one worker thread.
- 1 scheduling thread
- Runs the main loop
- Spawns work and I/O jobs
- Sends draw/compute calls to the GPU
- 1 I/O thread
- Blocks on calls to fread and fwrite
- Spawns work jobs for decoding
- 1 sound thread
- Runs from within OpenAL or satisfies SDL audio callbacks
- n worker threads, where n = ncores - 3
- Run serial work jobs (embarassingly parallel jobs will be run on the GPU)
Should the scheduling thread also be able to run work jobs? If so, is it safe enough to have any thread be able to send draw/compute calls to the GPU (using OpenGL 4)?
The reason you want as many worker threads as cores even though you have other threads, is the worker threads probably have a different workload than the task specific threads (or whatever the word for that is). Those threads may occasionally sleep, block on an IO signal, and under use a single core. You want your worker threads to saturate the remaining resources, so it's better to have too many worker threads some of the time, than too few at any time. You can, however, give task specific threads higher priority.