473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Please explain

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);
}
}
}
Nov 16 '05 #1
6 1490
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.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Zach" <no*@this.address> wrote in message
news:ce***************************@freeler.nl...
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);
}
}
}


Nov 16 '05 #2
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.

"Zach" <no*@this.address> wrote in message
news:ce***************************@freeler.nl...
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);
}
}
}

Nov 16 '05 #3
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:OU**************@TK2MSFTNGP14.phx.gbl...
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);
}

Nov 16 '05 #4

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#0**************@TK2MSFTNGP15.phx.gbl...
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.
Nov 16 '05 #5
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.

"Zach" <00@00.00> wrote in message
news:cc***************************@freeler.nl...

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#0**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 16 '05 #6
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#I**************@tk2msftngp13.phx.gbl...
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.
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
3
by: VijayShankar | last post by:
Can u be more specific on your question Anyway its not like Session variables are available for sometime and not available for sometime. When your session starts it is very much available...
1
by: Yash | last post by:
Hi, Can someone please explain to me what the StreamReader.DiscardBufferedData method does? The documentation says "Use DiscardBufferedData to seek to a known location in the underlying stream...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
12
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
9
by: colin.mcnulty | last post by:
Hi, I'm a SQL Server DBA, but I guess that won't buy me any friends round here huh? ;-) I've been asked to look at the SQL that's being executed on a DB2 database from a web app, specifically...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
2
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'}; printf("strlen=%d\n", strlen(a));...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.