A threading question re MSDN example

  • Thread starter Thread starter Zach
  • Start date Start date
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());
}
}
}
 
Zach,

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

Dan.
 
Back
Top