Something I couldnt see in there was deadlock. (sorry if I missed it or if you left it out for simplisity)
Deadlock is a trap for young players when multi-threading, it will cause both threads to grind to a halt indefinatly. Here is how it occurs...
As Thalassicus stated, to protect a variable that is used by more than one thread you have a lock for it.
If two different threads both want to change the same two variables they can result in deadlock.
E.G.
- First thread aquires lock for vairable A
- Second thread aquires a lock for variable B
Then
- First thread wants to aquire the lock for variable B
- AND Second thread wants to aquire the lock for variable A
NOW FIRST AND SECOND THREAD WAIT FOR EVER!
To avoid this (if you are new to multi-threading and dont know more advanced tactics) ALWAYS get locks for variables in order. If you want the locks on both A and B at the same time in a thread then always get A before you get B. If you do this, then only one thread can get to resources that both threads want!
Deadlock actualy occuring can be rare (you run your map hundreds of times before you finaly run out of luck and it occurs), so its nearly impossible to debug in something like starcraft!!! please avoid it in advance, because you will never find out whats wrong if you try and diagnose the problem debugging!!!
NB, this is also caused by more complex loops T1(wants A then B) T2(wants B then C) T3(wants C then A)... Again just re order to take A before B before C in your threads and you will be fine : )
Great post : P
Something I couldnt see in there was deadlock. (sorry if I missed it or if you left it out for simplisity)
Deadlock is a trap for young players when multi-threading, it will cause both threads to grind to a halt indefinatly. Here is how it occurs...
As Thalassicus stated, to protect a variable that is used by more than one thread you have a lock for it. If two different threads both want to change the same two variables they can result in deadlock.
E.G.
- First thread aquires lock for vairable A
- Second thread aquires a lock for variable B
Then
- First thread wants to aquire the lock for variable B
- AND Second thread wants to aquire the lock for variable A
NOW FIRST AND SECOND THREAD WAIT FOR EVER!
To avoid this (if you are new to multi-threading and dont know more advanced tactics) ALWAYS get locks for variables in order. If you want the locks on both A and B at the same time in a thread then always get A before you get B. If you do this, then only one thread can get to resources that both threads want!
Deadlock actualy occuring can be rare (you run your map hundreds of times before you finaly run out of luck and it occurs), so its nearly impossible to debug in something like starcraft!!! please avoid it in advance, because you will never find out whats wrong if you try and diagnose the problem debugging!!!
NB, this is also caused by more complex loops T1(wants A then B) T2(wants B then C) T3(wants C then A)... Again just re order to take A before B before C in your threads and you will be fine : )