L
Liviu Ursu
Hi, I am relatively new to NET Threading and learning from net examples and
books I got a problem that is not mentioned anyware.
Let's say thread1 does following:
Monitor.Enter(o);
try
{
Do X;
Monitor.Wait(o);
Do.Y;
}
finally
{
Monitor.Exit(o);
}
Thread2 does:
Monitor.Enter(o);
try
{
Do A;
Monitor.Pulse(o);
Do.B;
}
finally
{
Monitor.Exit(o);
}
On my dual core processor it happens sometimes that Thread2 ends before
Thread 1 starts, though they are started in order: Thread1, Thread2!!
And there is the problem:
At the moment Thread1 calls Monitor.Wait, thread2 is already finished, so
there is no thread that can call Pulse
So, Thread1 blocks forever.
QUESTION: Why does Monitor.Wait not acquire the lock back if no other
thread holds the lock.
How can I detect this situation and avoid it in an elegant way?
Regards
Liviu
books I got a problem that is not mentioned anyware.
Let's say thread1 does following:
Monitor.Enter(o);
try
{
Do X;
Monitor.Wait(o);
Do.Y;
}
finally
{
Monitor.Exit(o);
}
Thread2 does:
Monitor.Enter(o);
try
{
Do A;
Monitor.Pulse(o);
Do.B;
}
finally
{
Monitor.Exit(o);
}
On my dual core processor it happens sometimes that Thread2 ends before
Thread 1 starts, though they are started in order: Thread1, Thread2!!
And there is the problem:
At the moment Thread1 calls Monitor.Wait, thread2 is already finished, so
there is no thread that can call Pulse
So, Thread1 blocks forever.
QUESTION: Why does Monitor.Wait not acquire the lock back if no other
thread holds the lock.
How can I detect this situation and avoid it in an elegant way?
Regards
Liviu