Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple threads on the same hardware unit

My game uses a multi-threading architecture, with the threads laid out like this:

  • Main: responsible for the high-level architecture
  • Resources: responsible for asynchronous file I/O
  • Network: responsible for blocking network I/O
  • Workers: do CPU-intensive jobs

Right now, there are (hardware_threads - 3) worker threads, one for each unused hardware unit, but I'd like to add one more by combining the "Resources" and "Network" threads onto one unit since both threads are going to spend a lot of time idling.

  1. Is this possible in C++ with boost::thread?
  2. Is this even a worthwhile optimization?
like image 892
jmegaffin Avatar asked Dec 22 '12 14:12

jmegaffin


Video Answer


1 Answers

You're right, in that this is a bad idea. Having a one thread for one job design sounds simple, but it's really not- particularly when some jobs are heavier than others. Also, you have made not enough threads. Usually you want a couple more than that to account for slack in other threads which aren't running right now.

The fundamental problem here is that you are implementing your own threading backend. This is a bad idea. You need to move to something like TBB, which will deal with all of this stuff for you, and those engineers at Intel spent way, way longer profiling and working on it than you.

As for whether this is a worthwhile optimization, well, probably not. Just make enough workers and keep them loaded, and the resources/network thread which are blocking won't really make much difference.

like image 70
Puppy Avatar answered Oct 02 '22 15:10

Puppy