Please explain

Z

Zach

Could someone please explain what the
consequence of this //****** line is?

public void SecondThread()
{
lock(m_smplQueue)
{
Monitor.Pulse(m_smplQueue);
while(Monitor.Wait(m_smplQueue,1000))//******
{
int counter = (int)m_smplQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(m_smplQueue);
}
}
}
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

It instructs to wait for EITHER m_smplQueue to become signaled OR the one
second timeout to occur. If the Wait calls returns due to timeout, it
returns false, thus preventing the execution flow from entering the while
loop.
 
B

Bob Powell [MVP]

The Monitor class is waiting on the queue object with a timeout of 1000
milliseconds.

This thread and some other(s) are synchronised on the queue object.

From MSDN...

When a thread calls Wait, it releases the lock on the object and enters the
object's waiting queue. The next thread in the object's ready queue (if
there is one) acquires the lock and has exclusive use of the object. The
thread that invoked Wait remains in the waiting queue until either a thread
that holds the lock invokes PulseAll, or it is the next in the queue and a
thread that holds the lock invokes Pulse. However, if millisecondsTimeout
elapses before another thread invokes this object's Pulse or PulseAll
method, the original thread is moved to the ready queue in order to regain
the lock. If the condition in the object's state has not been met, the
thread might call Wait again to reenter the waiting queue until it has been
met.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Z

Zach

Dmitriy Lapshin said:
Hi,

It instructs to wait for EITHER m_smplQueue to become signaled OR the one
second timeout to occur. If the Wait calls returns due to timeout, it
returns false, thus preventing the execution flow from entering the while
loop.

As I understand the code, the contents of the loop
will be activated when waiting is true. But that will
happen only once because of the Pulse. Since it is
a one-off action, I fail to see the use of the "while".

And, please, what is the difference (apart from the
time-out) between:

(A)

Monitor.Pulse(m_smplQueue);
while(Monitor.Wait(m_smplQueue,1000))//******
{
int counter = (int)m_smplQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(m_smplQueue);
}

and:

(B)

(there are MAX elements in the queue)

Monitor.Pulse(m_smplQueue);
int ctr = 0;
while(ctr++ < MAX)
{
Monitor.Wait(m_smplQueue);
int counter = (int)theQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(m_smplQueue);
}
 
Z

Zach

Bob Powell said:
The Monitor class is waiting on the queue object with a timeout of 1000
milliseconds.

This thread and some other(s) are synchronised on the queue object.

From MSDN...

When a thread calls Wait, it releases the lock on the object and enters the
object's waiting queue. The next thread in the object's ready queue (if
there is one) acquires the lock and has exclusive use of the object. The
thread that invoked Wait remains in the waiting queue until either a thread
that holds the lock invokes PulseAll, or it is the next in the queue and a
thread that holds the lock invokes Pulse. However, if millisecondsTimeout
elapses before another thread invokes this object's Pulse or PulseAll
method, the original thread is moved to the ready queue in order to regain
the lock. If the condition in the object's state has not been met, the
thread might call Wait again to reenter the waiting queue until it has been
met.

Hi Bob,

So Wait = GOTO WaitQ
So Pulse = LEAVE WaitQ + GOTO ReadyQ

while (waiting, but stop waiting after a second)
{
if(it is true that you are waiting) do something
Pulse = stop doing the something
}

Why a loop? The action takes place only once?

Zach.
 
B

Bob Powell [MVP]

This code you've posted comes directly from the Monitor sample where it is
well commented as to exactly what's going on.

See MSDN and the Monitor.Wait method. The code in that page documents
exactly what's happening.

Did you get your code from somewhere other than MSDN? If you did, whoever
wrote it just copied the sample and took out the useful bits, ie, the
comments.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Z

Zach

Bob Powell said:
This code you've posted comes directly from the Monitor sample where it is
well commented as to exactly what's going on.

I understand that {the action}depends on waiting, which terminates
immediately due to the subsequent Pulse. So the action is one-off,
so why a loop. That is my question, in spite of the excellent comment
in MSDN which I have seen, and do not understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top