|
|
It's a timing issue, mainly. Multi-threading is mainly needed when processes need to run concurrently (say client updates to a server).
This probably won't be the case in a single linked list of objects. More likely is the need to run object functions at different times - say you have update functions that need only run once every 30th of a second, but animation functions that need to run every 60th of a second.
To accomplish that you just need to track two different "tick" counters. When you hit your 60th of a second tick you iterate through your list and only "fire" the functions that use that counter, ignoring the update functions that fire every 30th of a second. The NEXT time through, BOTH your counters will be valid, so you fire-off both types of functions.
You might want to stagger the timings on these counters so they don't both fire at once. So when your 30th/second counter fires, your 60th/second counter might be at say 45. Then you never have both updates and animation firing on the same pass through the list. Obviously you must be able to iterate through the list before the next "tick" occurs on either timer.
But however you handle this timing, iterating through the list is a sequential process - you aren't going to fire object A's update on a thread, then while that update is running continue through the list and fire object B's update. You will finish object A, then iterate to object B. The only case for multi-threading here is if you want to be able to run more than one iteration of the list at the same time - but generally your animations will change as a result of your updates, so it is better to stick to sequential processing. Just remember that if two functions in two separate threads access the same memory, the first to access it will have it locked, and the second thread will have to wait. This may actually degrade performance.
You will probably find the slight performance increase using threads this way to be not worth the complexity of management. A better way to use threads is for separate sub-systems: a thread for user input via keyboard and mouse, another thread for timing, etc. And these aren't processes that are connected to individual objects, but are components of the engine. they will usually work in entirely separate memory spaces, so memory locking and the idle time it can cause aren't a problem. Take the case of user input: it will ultimately update some properties/variables that will be applied to objects, but by careful thread management the user input thread will only WRITE these, and the object update thread will only READ them, keeping memory collision to a minimum.
|