473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sorry, the question remains

The code below was taken from an MSDN example.
The example contains explantory lines. Nevertheless
I do not understand the use of the while loop. Because
of the Pulse within the loop, I would expect the loop
to end after the dequeue. If that is correct this would
be one off situation, I therefore don't understand why
a loop was used.

There are two threads in the example.

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
10 1127
I don't believe the while loop is used to iterate through items in the
queue; the two comment lines in the original MSDN article really explain
what the while loop is doing:

//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.

Basically, the while loop is causing a halt in execution (on the current
thread), until such time as the current thread can get an exclusive lock
on the m_smplQueue object, or 1 second of time passes (whichever occurs
first). This is carried out by the Monitor.Wait(m_smplQueue,1000) call.
If an exclusive lock is acheived, the code inside the loop runs. If
an exclusive lock is NOT achieved, essentially, the method ends.

Here is a link to the full sample source:
http://msdn.microsoft.com/library/de...pulsetopic.asp

And, the description of the Monitor.Wait method:
http://msdn.microsoft.com/library/de...WaitTopic2.asp

HTH...
Chris
Nov 16 '05 #2
After the dequeue, the thread loops back to wait for its next turn again.

"Zach" wrote:
The code below was taken from an MSDN example.
The example contains explantory lines. Nevertheless
I do not understand the use of the while loop. Because
of the Pulse within the loop, I would expect the loop
to end after the dequeue. If that is correct this would
be one off situation, I therefore don't understand why
a loop was used.

There are two threads in the example.

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

"Tu-Thach" <Tu*****@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
After the dequeue, the thread loops back to wait for its next turn again.


I think the penny has finally dropped:
Because of the Pulse, the wait-status is maintained, whilst at
the same time the other thread is activated. The loop continues
after the other thread Pulses. Right?
Nov 16 '05 #4
"Chris Hyde" <ch*****@nodirecwayspam.net> wrote in message
news:#N**************@TK2MSFTNGP15.phx.gbl...
I don't believe the while loop is used to iterate through items in the
queue; the two comment lines in the original MSDN article really explain
what the while loop is doing:

//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.

Yes, you are right, the example contains comment.
I said in my posting(s) that I was aware of that comment,
but that I didn't understand it. But I think a glass of wine
helped :) See my response to Tu-Thach. See if you agree
with what I said to him, and thanks for your response.

Zach.
Nov 16 '05 #5
Chris Hyde <ch*****@nodirecwayspam.net> wrote:
I don't believe the while loop is used to iterate through items in the
queue; the two comment lines in the original MSDN article really explain
what the while loop is doing:

//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.

Basically, the while loop is causing a halt in execution (on the current
thread), until such time as the current thread can get an exclusive lock
on the m_smplQueue object, or 1 second of time passes (whichever occurs
first). This is carried out by the Monitor.Wait(m_smplQueue,1000) call.
If an exclusive lock is acheived, the code inside the loop runs. If
an exclusive lock is NOT achieved, essentially, the method ends.


No, that's not quite right. The thread will re-acquire the lock
*either* way before the Wait method returns. It's whether or not
m_smplQueue has been pulsed or not since the Wait started which is
being tested.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No, that's not quite right. The thread will re-acquire the lock
*either* way before the Wait method returns. It's whether or not
m_smplQueue has been pulsed or not since the Wait started which is
being tested.


I was assuming that the loop will do go-arounds when
/ as soon as, it is able to do so, given the action in the
other thread.
Nov 16 '05 #7
Zach <00@00.00> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No, that's not quite right. The thread will re-acquire the lock
*either* way before the Wait method returns. It's whether or not
m_smplQueue has been pulsed or not since the Wait started which is
being tested.


I was assuming that the loop will do go-arounds when
/ as soon as, it is able to do so, given the action in the
other thread.


It will, as soon as it is able to do so - but Wait will not return
until the lock has been reacquired, whether or not the monitor was
pulsed in the meantime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zach <00@00.00> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No, that's not quite right. The thread will re-acquire the lock
*either* way before the Wait method returns. It's whether or not
m_smplQueue has been pulsed or not since the Wait started which is
being tested.


I was assuming that the loop will do go-arounds when
/ as soon as, it is able to do so, given the action in the
other thread.


It will, as soon as it is able to do so - but Wait will not return
until the lock has been reacquired, whether or not the monitor was
pulsed in the meantime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


OK, thanks,
Zach.
Nov 16 '05 #9
> It will, as soon as it is able to do so - but Wait will not return
until the lock has been reacquired, whether or not the monitor was
pulsed in the meantime.


What about the 1000 ms timeout though? What if the lock has not been
reacquired before the timeout is reached (ok, probably not going to
happen in the simple example, but what about in a similar situation
where perhaps MORE intensive work was being performed)?

Chris
Nov 16 '05 #10
Chris Hyde <ch*****@nodirecwayspam.net> wrote:
It will, as soon as it is able to do so - but Wait will not return
until the lock has been reacquired, whether or not the monitor was
pulsed in the meantime.


What about the 1000 ms timeout though? What if the lock has not been
reacquired before the timeout is reached (ok, probably not going to
happen in the simple example, but what about in a similar situation
where perhaps MORE intensive work was being performed)?


Then it waits until it's reacquired before returning. If something else
has the lock out indefinitely, the method won't return. The MSDN docs
are currently confusing on this matter, and are being reworked.
(There's a thread in .framework about this somewhere, although I can't
remember the name.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

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

Similar topics

12
by: David Lindsay | last post by:
I want to put date and time on my web page, and to be sure it tracks the BST/ GMT changes. Can anyone help me, I have gone back in my historic file for quite a while and not found anything. Sorry...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
5
by: pitdog | last post by:
Hello, I am unable to find information on about this issue so I thought I would post and ask. I have a class that is using the sigleton pattern as it has an internal static instance of...
4
by: Lee | last post by:
Hello all, Please excuse me if this is not the right group to ask in. Assume that I have a Person object that reflects data held in a datastore (DB, XML FIles, etc). Gernally, where should...
4
by: Diffident | last post by:
Hello All, I have a question which is pertinent to Page's lifecycle. I declared a protected static object (global variable within that class) whose value is set only once when the page is...
6
by: Andrew Neillans | last post by:
Hi all, First off, I apologise if this is mentioned in a MSDN document somewhere, but I've searched both MSDN and Google and can't find anything - so thought I'd post. Ok, now the problem. ...
11
by: Dilip | last post by:
Howdy I have code similar to this in my project: Note: BSTR is an abomination conjured up some disturbed person in the COM world. BSTR strings must be allocated/deallocated using the...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
5
by: raj | last post by:
I know what interfaces are and what they used for etc. Today i am learning about serilization. I know to mark the class "serializable" and implement ISerializable interface, and then implement...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.