Feature Post

Top

Producer consumer architecture

Typical example; Producer- consumer multi threaded architecture.

Couple of months ago I came across a problem that I solved by using this architecture. Following is the code that dealt with the following solution,

For example, there are two guys and a list; guy1 requests for the appointment scheduling for probably a doctor, request gets added to a list/queue, and then the guy2 in turn looks for the appointment requests in that list/queue and sends them for processing one by one accordingly.

I’ll add a visual soon to provide a better understanding. This is upon my friend, Talpur’s, request.


/* *********************************************************************************************************
*
* Class Name : CApptScheduler
* Filename : CApptScheduler.cs
*
* Author : Kamran Khan - KAMRANHK_AT_gmail.com
* Date : Snuday, Mar 23, 2008
*
* Copyright : (c) Copy left.
* http://izlooite.blogspot.com
*
* Disclaimer : THE INFORMATION FROM OR THROUGH THE SITE IS PROVIDED “AS IS,” “AS AVAILABLE,” AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, ARE DISCLAIMED (INCLUDING, BUT NOT LIMITED TO, THE DISCLAIMER OF ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE).
* THE INFORMATION AND SERVICES MAY CONTAIN BUGS, ERRORS, PROBLEMS OR OTHER LIMITATIONS.
* ALL RESPONSIBILITY OR LIABILITY FOR ANY DAMAGES CAUSED BY VIRUSES CONTAINED WITHIN THE ELECTRONIC
* FILE CONTAINING THE FORM OR DOCUMENT IS DISCLAIMED.
*
*
* Type : Backend processing, the thread.
* Description : This object should be able to handling the scheduling of appointment, without
* a glitch over front end. The reason of this object is to keep the appointment
* scheduling entirely separate from the gui. Just the click of the button should
* enqueue the plan in this object, and this object will handle the scheduling of
* appointment.
*
* References :
*
* Révision History :
* -----------------------------------------------------------------------------------------------
* DATE OWNER DESCRIPTION
* -----------------------------------------------------------------------------------------------
* Mar-23-07 KHK -Added the class
* -Added methods, the skeleton code
* -Add the queue manager, that will handle the tasks, the Plan objects.
*
*/


using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Business.Controls.Planning
{
public class CApptScheduler : IDisposable
{
private object theLocker = new object(); //Locking mechanism
private Queue m_theQueue = new Queue();//List that would contain the jobs
Thread[] m_theWorkers;//Worker threads

///
/// Schedules the appointment; the producer.
///

/// Worker count, to start with
public CApptScheduler(int nWorkerCount)
{
m_theWorkers = new Thread[nWorkerCount];

for (int i = 0; i < nWorkerCount; i++)
{
(m_theWorkers[i] = new Thread(Consume)).Start();
}

}

///
/// Schedules an appointment. Consumer
///

private void Consume()
{
while (true)
{
//CPlan thePlan = null;
lock (theLocker)
{
while (m_theQueue.Count == 0) Monitor.Wait(theLocker);
//here perform the appointment scheduling.
{
//here look for the appointment type and schedule accordingly.
//regular, create update cancel
//content, create update cancel.
ScheduleThis(m_theQueue.Dequeue());
}
}
if (m_theQueue.Peek() == null) //nothing found.
return; //exit!
}

}

///
/// Schedules the appointment; the one that gets called during consumption.
///

/// The plan.
private void ScheduleThis(string thePlan)
{
throw new Exception("The method or operation is not implemented.");
}

///
/// Enqueues the appt.
///

/// The plan.
public void EnqueueAppt(string thePlan)
{
lock (theLocker)
{
m_theQueue.Enqueue(thePlan);//Add the plan into list
Monitor.PulseAll(theLocker);
}
}

#region IDisposable Members

public void Dispose()
{
foreach (Thread worker in m_theWorkers) EnqueueAppt(null);
foreach (Thread worker in m_theWorkers) worker.Join();
}

#endregion

#region IDisposable Members

void IDisposable.Dispose()
{
throw new Exception("The method or operation is not implemented.");
}

#endregion


}
}