471,610 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Monitor.Pulse question

Hi, Below is a hacked up version of an example I found on MSDN. My only
change was to add a third thread and some Sleeps. That's where my
question(s) comes in.
In the original it seems to work because Thread1 waits on Thread2 and
vice versa, although I can't quite see how.
Disabling Thread3 and leaving the sleep in works fine, processing is
nicely throttled. Activating Thread3 and we enter a world of pain. I
don't understand what the first Pulse is doing in Thread2, I think I
understand the second one, and the purpose of the Wait, although how
it's reached inside the Lock statement confuses me.

Here's the code,it should work pasted verbatim into a console app. If
someone has a simple explanation of what Pulse is supposed to do, and
how the various competing lock statements interact, or even how to make
the code below function (ie Thread1 pumps out stuff, Thread2 and 3
consume it when they can), then I'll be a happy bunny.

Thanks.

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

namespace MonitorCS1
{
class MonitorSample
{
const int MAX_LOOP_TIME = 1000;
Queue m_smplQueue;
Random r = new Random(1);

public MonitorSample()
{
m_smplQueue = new Queue();
}
public void FirstThread()
{
int counter = 0;
lock(m_smplQueue)
{
while(counter < MAX_LOOP_TIME)
{
//Wait, if the queue is busy.
Monitor.Wait(m_smplQueue);
//Push one element.
m_smplQueue.Enqueue(counter);
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);

counter++;
}
}
}
public void SecondThread()
{
lock(m_smplQueue)
{
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.
while(Monitor.Wait(m_smplQueue,1000))
{
//Pop the first element.
int counter = (int)m_smplQueue.Dequeue();
//Print the first element.
Console.WriteLine("Second " + counter.ToString());
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
Thread.Sleep(r.Next(20));
}
}

}
public void ThirdThread()
{
lock(m_smplQueue)
{
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.
while(Monitor.Wait(m_smplQueue,1000))
{
//Pop the first element.
int counter = (int)m_smplQueue.Dequeue();
//Print the first element.
Console.WriteLine("Third " + counter.ToString());
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
Thread.Sleep(r.Next(20));
}
}
}
//Return the number of queue elements.
public int GetQueueCount()
{
return m_smplQueue.Count;
}

static void Main(string[] args)
{
//Create the MonitorSample object.
MonitorSample test = new MonitorSample();
//Create the first thread.
Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
//Create the second thread.
Thread tSecond = new Thread(new ThreadStart(test.SecondThread));

Thread tThird = new Thread(new ThreadStart(test.ThirdThread));
//Start threads.
tFirst.Start();
tSecond.Start();
tThird.Start();
//wait to the end of the two threads
tFirst.Join();
tSecond.Join();
tThird.Join();
//Print the number of queue elements.
Console.WriteLine("Queue Count = " +
test.GetQueueCount().ToString());
}
}
}

Nov 14 '06 #1
2 2384
Solved it... Sort of,Replacing the While in Thread2 and 3 with:

while(Monitor.Wait(m_smplQueue))
{
if(m_smplQueue.Count>0)
{
//Pop the first element.
int counter = (int)m_smplQueue.Dequeue();
//Print the first element.
Console.WriteLine("Third " + counter.ToString());
//Release the waiting thread.
}
Monitor.Pulse(m_smplQueue);
Thread.Sleep(r.Next(50));
}

Solves the problem, I still don't know what the first Pulse is for
though in Threads 2 and 3.
Thanks

Nov 14 '06 #2
Just read this:
http://pluralsight.com/blogs/mike/ar...2/13/3905.aspx which
strangely cleared up my confusion. I'd already figured out I had picked
the wrong direction to expand the demo code in, I was trying for
concurrency instead of serialisation. As soon as I saw the above it all
clicked into place :)

Nov 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by bughunter | last post: by
25 posts views Thread by vooose | last post: by
1 post views Thread by Mike | last post: by
12 posts views Thread by Perecli Manole | last post: by
reply views Thread by Perecli Manole | last post: by
reply views Thread by MichaelMortimer | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.