I am learning MT (using boost.threads) right now. And I have been google-ing all day today about Multithreading, its issues and how to prevent them. But, I still don't understand one thing about communication between threads. I am trying to make an event handler to communicate between threads but I am getting confused everytime i start to think about it. Here is my scenario:
I have 3 threads for now: Core, Graphics, and Scripting
Each thread has a game system. Game systems have functions to handle the events sent by the components. The events are always read only (they are ONLY created by the Component).
Core Thread:
It runs Logger, Filesystem, Input, Game Entities (should I change it to a new thread?)
Graphics Game System (also thread):
Contains all the instances of models, animations etc (using OSG) and renders every graphics related object
Scripting Game System (also thread):
Contains and Runs Python Scripts.
Everything works asynchronously. But here is whats confusing me and not letting me move forward.
I have a HealthComponent and for testing purposes I put:
if(parent->attribute("health") < 50) {
GameSystemHandler::passEvent(new Event1<String>("ChangeAnimation", "AnimationCrippling")); //it sends events to all the game systems (is in the core thread).
}
Now, if two entities got shot hard that they lost 70 health out of 100, How GraphicsSystem is going to take care of changing both entities' animations. Isn't it going to overlap??
And the same for Scripting. If two of them says to run some script that has:
engine.current_object.doSomething();
How does the threading know which current_object it is?
In both of these casec, do I have to use mutexes to stop one until the other one finishes? If I do, wouldn't that be a HUGE bottleneck and MT is going to be the same performance of a single threaded environment (concurrency is going to get destroyed due to the syncs).
I am really new to the MT theory. Please help me out!
Thanks in advance,
Gasim