A threading question re MSDN example

Z

Zach

Re:
http://msdn.microsoft.com/library/default.asp?url=/library/en-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());
}
}
}
 
D

Dan Bass

Zach,

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

Dan.
 

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

Similar Threads


Top