A
AliRezaGoogle
Dear Members,
I have a problem in the concepts of Monit.Enter and Monitor.Exit
(Before everything I should say that I know how to solve this problem
by
using Monitor.Wait and I do not need a solution. But this question
sticks to
my head as a conceptual problem)
Suppose there are two threads T1, T2 running concurrently:
T1()
{
While(true){
Monit.Enter(MyObjec);
{
....
}
Monitor.Exit(MyObjec);
}
}
T2()
{
While(true){
Monit.Enter(MyObjec);
{
....
}
Monitor.Exit(MyObjec);
}
}
Suppose T1 runs before T2 a little sooner.
After T1 gains the lock on MyObjec, T2 comes in and tries to get
access to
MyObjec too. Since T1 called Monitor.Enter(MyObjec) previously T2 will
be
blocked ( but as what MSDN says, it still remains in ready queue).
After a
while, T1 calls Monitor.Exit(MyObjec). So T2 can gain access to
MyObjec.
But surprisingly it cannot. Because T1 rapidly gains access to MyObjec
again.
It seems that while loop cycles too fast to let T2 gain the lock on
MyObjec. I
am not sure this is the matter of speed but I am sure that there is no
need
to call Monitor.Pulse(MyObjec) to notify T2 because T2 is still in
ready queue
(and Monitor.Pulse(MyObjec) just affects waiting queue).
What is the problem? Is this the matter of speed? Am I wrong about
the
concepts of Monitor methods?
Thanks in advance.
I have a problem in the concepts of Monit.Enter and Monitor.Exit
(Before everything I should say that I know how to solve this problem
by
using Monitor.Wait and I do not need a solution. But this question
sticks to
my head as a conceptual problem)
Suppose there are two threads T1, T2 running concurrently:
T1()
{
While(true){
Monit.Enter(MyObjec);
{
....
}
Monitor.Exit(MyObjec);
}
}
T2()
{
While(true){
Monit.Enter(MyObjec);
{
....
}
Monitor.Exit(MyObjec);
}
}
Suppose T1 runs before T2 a little sooner.
After T1 gains the lock on MyObjec, T2 comes in and tries to get
access to
MyObjec too. Since T1 called Monitor.Enter(MyObjec) previously T2 will
be
blocked ( but as what MSDN says, it still remains in ready queue).
After a
while, T1 calls Monitor.Exit(MyObjec). So T2 can gain access to
MyObjec.
But surprisingly it cannot. Because T1 rapidly gains access to MyObjec
again.
It seems that while loop cycles too fast to let T2 gain the lock on
MyObjec. I
am not sure this is the matter of speed but I am sure that there is no
need
to call Monitor.Pulse(MyObjec) to notify T2 because T2 is still in
ready queue
(and Monitor.Pulse(MyObjec) just affects waiting queue).
What is the problem? Is this the matter of speed? Am I wrong about
the
concepts of Monitor methods?
Thanks in advance.