The following example of rudimentary thread pool is 'bluntly' copied
from "Accelerate d C#" book. My questions follow the code:
using System;
using System.Threadin g;
using System.Collecti ons;
public class CrudeThreadPool
{
static readonly int MAX_WORK_THREAD S = 4;
static readonly int WAIT_TIMEOUT = 2000;
public delegate void WorkDelegate();
public CrudeThreadPool () {
stop = 0;
workLock = new Object();
workQueue = new Queue();
threads = new Thread[ MAX_WORK_THREAD S ];
for( int i = 0; i < MAX_WORK_THREAD S; ++i ) {
threads[i] =new Thread( new
ThreadStart(thi s.ThreadFunc) );
threads[i].Start();
}
}
private void ThreadFunc() {
lock( workLock ) {
int shouldStop = 0;
do {
shouldStop = Interlocked.Exc hange( ref stop,stop );
if( shouldStop == 0 )
{
WorkDelegate workItem = null;
if( Monitor.Wait(wo rkLock, WAIT_TIMEOUT) ) {
// Process the item on the front of the
queue
lock( workQueue ) {
workItem =(WorkDelegate)
workQueue.Deque ue();
}
workItem();
}
}
} while( shouldStop == 0 );
}
}
public void SubmitWorkItem( WorkDelegate item ) {
lock( workLock ) {
lock( workQueue ) {
workQueue.Enque ue( item );
}
Monitor.Pulse( workLock );
}
}
public void Shutdown() {
Interlocked.Exc hange( ref stop, 1 );
}
private Queue workQueue;
private Object workLock;
private Thread[] threads;
private int stop;
}
public class EntryPoint
{
static void WorkFunction() {
Console.WriteLi ne( "WorkFuncti on() called on Thread {0}",
Thread.CurrentT hread.GetHashCo de() );
}
static void Main() {
CrudeThreadPool pool = new CrudeThreadPool ();
for( int i = 0; i < 10; ++i ) {
pool.SubmitWork Item(new
CrudeThreadPool .WorkDelegate(E ntryPoint.WorkF unction) );
}
pool.Shutdown() ;
}
}
SO my question is why do we need need workLock, can we just use
workQueue to synchronize the access to the queue and manage the
execution of threads in the pool?
Thanks
from "Accelerate d C#" book. My questions follow the code:
using System;
using System.Threadin g;
using System.Collecti ons;
public class CrudeThreadPool
{
static readonly int MAX_WORK_THREAD S = 4;
static readonly int WAIT_TIMEOUT = 2000;
public delegate void WorkDelegate();
public CrudeThreadPool () {
stop = 0;
workLock = new Object();
workQueue = new Queue();
threads = new Thread[ MAX_WORK_THREAD S ];
for( int i = 0; i < MAX_WORK_THREAD S; ++i ) {
threads[i] =new Thread( new
ThreadStart(thi s.ThreadFunc) );
threads[i].Start();
}
}
private void ThreadFunc() {
lock( workLock ) {
int shouldStop = 0;
do {
shouldStop = Interlocked.Exc hange( ref stop,stop );
if( shouldStop == 0 )
{
WorkDelegate workItem = null;
if( Monitor.Wait(wo rkLock, WAIT_TIMEOUT) ) {
// Process the item on the front of the
queue
lock( workQueue ) {
workItem =(WorkDelegate)
workQueue.Deque ue();
}
workItem();
}
}
} while( shouldStop == 0 );
}
}
public void SubmitWorkItem( WorkDelegate item ) {
lock( workLock ) {
lock( workQueue ) {
workQueue.Enque ue( item );
}
Monitor.Pulse( workLock );
}
}
public void Shutdown() {
Interlocked.Exc hange( ref stop, 1 );
}
private Queue workQueue;
private Object workLock;
private Thread[] threads;
private int stop;
}
public class EntryPoint
{
static void WorkFunction() {
Console.WriteLi ne( "WorkFuncti on() called on Thread {0}",
Thread.CurrentT hread.GetHashCo de() );
}
static void Main() {
CrudeThreadPool pool = new CrudeThreadPool ();
for( int i = 0; i < 10; ++i ) {
pool.SubmitWork Item(new
CrudeThreadPool .WorkDelegate(E ntryPoint.WorkF unction) );
}
pool.Shutdown() ;
}
}
SO my question is why do we need need workLock, can we just use
workQueue to synchronize the access to the queue and manage the
execution of threads in the pool?
Thanks
Comment