473,398 Members | 2,188 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,398 software developers and data experts.

A threading question re MSDN example

Re:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemThreadingMonitorClassPulseTopic.asp

Why is the example shown there done as it is, and not as below?
(I mean the logic not the little changes in nomenclature)

using System;
using System.Threading;
using System.Collections;

namespace MonitorCS1
{
class MonitorSample
{
const int max = 1000;
Queue theQueue;

public MonitorSample()
{
theQueue = new Queue();
}

public void FirstThread()
{
int counter = 0;
lock(theQueue)
{
while(counter < max)
{
Monitor.Wait(theQueue);
theQueue.Enqueue(counter);
Monitor.Pulse(theQueue);
counter++;
}
}
}

public void SecondThread()
{
lock(theQueue)
{
Monitor.Pulse(theQueue);
int ctr = 0;
while(ctr++ < max)
{
Monitor.Wait(theQueue);
int counter = (int)theQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(theQueue);
}
}
}

public int GetQueueCount()
{
return theQueue.Count;
}

static void Main()
{
MonitorSample test = new MonitorSample();
Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
tFirst.Start();
tSecond.Start();
tFirst.Join();
tSecond.Join();
Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
}
}
}


Nov 16 '05 #1
1 1748
Zach,

Having just compared them and I'm stuggling to see any difference in logic
at all... What are you referring to?

Dan.

"Zach" <00@00.00> wrote in message
news:e3**************************@freeler.nl...
Re:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemThreadingMonitorClassPulseTopic.asp

Why is the example shown there done as it is, and not as below?
(I mean the logic not the little changes in nomenclature)

using System;
using System.Threading;
using System.Collections;

namespace MonitorCS1
{
class MonitorSample
{
const int max = 1000;
Queue theQueue;

public MonitorSample()
{
theQueue = new Queue();
}

public void FirstThread()
{
int counter = 0;
lock(theQueue)
{
while(counter < max)
{
Monitor.Wait(theQueue);
theQueue.Enqueue(counter);
Monitor.Pulse(theQueue);
counter++;
}
}
}

public void SecondThread()
{
lock(theQueue)
{
Monitor.Pulse(theQueue);
int ctr = 0;
while(ctr++ < max)
{
Monitor.Wait(theQueue);
int counter = (int)theQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(theQueue);
}
}
}

public int GetQueueCount()
{
return theQueue.Count;
}

static void Main()
{
MonitorSample test = new MonitorSample();
Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
tFirst.Start();
tSecond.Start();
tFirst.Join();
tSecond.Join();
Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
}
}
}

Nov 16 '05 #2

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

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
8
by: Mahesh Devjibhai Dhola [MVP] | last post by:
We are building Chat like application using Forms and as a result our programming is becoming complicated to display messages received on different threads in the chat window (due to STA...
9
by: Edward | last post by:
Hello I hope someone could help me I'm trying to prevent code from running before the thread I created completes. Here's the code snippet DataTransformerWorker dtw = new...
1
by: craigedmunds | last post by:
Was having a few problems with some threading issues following the microsoft support webcast Microsoft ASP.NET Threading. Spoke to Wade Mascia & managed to get clarification. Thought i'd share it...
8
by: VB Programmer | last post by:
How do I put a timer in it's own thread? The reason I need this is that I may have like 30 timers going continuously and it's more efficient (right??) Thanks.
7
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, ...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
15
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
3
by: Richard MSL | last post by:
I have a C# application that works, with a main menu where the user can choose different options that run, and then return to the menu. I want to change it so that the user can starting running an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.