I have 2 methods, each on a different thread.
While Method1 is on its critical section, Method2 needs to wait.
Notice that Method2 runs constantly and needs to wait ONLY in case Method1 is operating on its critical section. Otherwise, just continue with its work..
How do I do that?
Pseudo-Code:
Method1
{
Announce the entrence to critical-section
......Do some stuff.........
Announce the leaving of critical-section
}
Method2
{
Method1 is in its critical-section? - WAIT TILL IT DONE
......Do some stuff...........
}
You should be using a condition variable to reliably control the behavior between 2 threads. Technically you can do what the other answers suggest with just sharing a variable but it will fall apart quickly in other scenarios.