Click here to Skip to main content
11,711,750 members (72,244 online)
Click here to Skip to main content

Work Queue

, 1 Feb 2003 200K 6.2K 109
Rate this:
Please Sign up or sign in to vote.
Simple and elegant thread pool.
<!-- Article image -->

Sample Image - Work_Queue.gif

<!-- Add the rest of your HTML here -->

Introduction

In real time systems that need to respond to and process different tasks in the fastest possible way, and the tasks can be processed concurrently, there needs to be a number of thread waiting, willing, and able to perform the tasks at hand. This can be achieved with a specific structural scheme for each specific job, or with a generic method called thread pool. The work queue is a simple and elegant type of thread pool that creates requested number of threads at its creation and manages a queue of different work items that implement the specific tasks, where each work item in his turn gets a thread that works and processes it.

How to Use

First a new type of work item needs to be defined in a format that can be processed by the work queue.   To do this, you need to implement a work item class that inherits from the base work item class WorkItemBase. The WorkItemBase has two abstract functions that need to be implemented.  The first, DoWork, is the method that will be executed by the free thread when the work item gets its turn in the queue.  The second, Abort, is called when the queue is destroyed prior to the work item getting its turn. The specific work item should be implemented as follows.

class SpecificWorkItem : public WorkItemBase
{
    void   DoWork(void* pThreadContext);
    void   Abort();

 //memeber variables needed here
};

void   SpecificWorkItem::DoWork(void* pThreadContext)
{
  //Notify Start
  //proccessing done here
  //Notify Finish
  //free all that was occupied
}

void   SpecificWorkItem::Abort()
{
  //Notify aborted
  //free all that was occupied
}

Note that a different types of work items can be processed in the same work queue.  In the work queue, first the Create function needs to be called where the first parameter is the number of threads and the second is an array of pointers to a thread data structure.  This structures can be used as a working surface for each of the threads. This is used to reduce the local variables declared in the DoWork function and the dynamic allocation for each item that is being processed, for optimization of processing.

CWorkQueue WorkQueue;
WorkQueue.Create(5);

As you can see in the code, one can ignore the second parameter which has the default value is NULL. Next, to insert a work item into the queue you need to call InsertWorkItem with the newly created work item. This function can be called asynchronously.

SpecificWorkItem* pWorkItem = new SpecificWorkItem()
// prepare the work item for the task
m_WorkQueue.InsertWorkItem(pWorkItem);

After using the work queue you must call the Destroy function. This function will wait until all threads have finished processing the work items that are already out of the queue, then calls the Abort function of each of the work items that are waiting in the queue to be processed.

m_WorkQueue.Destroy();

Implementation

The work queue is implemented using an STL queue of work items which is guaranteed to work in an asynchronous system by a mutual exclusion lock, a semaphore that is initialized with an initial count of zero, an abort event, and an array of threads that are created to run a particular function.  This function is waiting on the two asynchronous objects.   First the abort event that, if set, the thread will return from the function.  Second, semaphore, that if released, the thread will extract the next item in the queue and call its DoWork function. At the insert function the semaphore is released (by 1) after inserting the work item, allowing one of the threads to do attach to the worker function. When the Destroy function is called in turn, the abort event is set and the function waits for all threads to terminate.  For each of the work item that are left in the queue, it calls their Abort function.

Demo project

The demo project is a project I included merely to illustrate the way the work queue performs. What you need to do to work it is:

  1. determine the number of thread you want
  2. click the create button to create the work queue
  3. insert work item to the queue as you please by clicking the insert item button
  4. destroy the work queue click the destroy button.

Enjoy.

thanks to Hans Dietrich for the XlistCtrl

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Uri Twig
Web Developer
Israel Israel
4 years expirience coding C++ with MFC & STL and coding C for Windows XP/2K internals (Drivers).

I Love what I do.

For pastime activities:
Fun & Games

You may also be interested in...

Comments and Discussions

 
GeneralNice! Pin
wxf0410415-Sep-13 23:10
memberwxf0410415-Sep-13 23:10 
QuestionNice article but ? Pin
Le@rner17-Oct-10 20:36
memberLe@rner17-Oct-10 20:36 
Generalgreat job :) Pin
alejandro29A29-Dec-08 1:26
memberalejandro29A29-Dec-08 1:26 
GeneralMessages in the worker thread Pin
chocm14-Oct-08 21:30
memberchocm14-Oct-08 21:30 
General....very nice code!! Pin
Andrea7524-Nov-05 21:39
memberAndrea7524-Nov-05 21:39 
GeneralCancel an expecific thread Pin
Desvariovsk15-Oct-05 6:40
memberDesvariovsk15-Oct-05 6:40 
QuestionSend cancel from GUI? Pin
ehh8-Sep-05 5:57
memberehh8-Sep-05 5:57 
QuestionRe: Send cancel from GUI? Pin
ehh8-Sep-05 6:20
memberehh8-Sep-05 6:20 
GeneralUnzip Pin
Danny Maes11-Aug-05 21:22
memberDanny Maes11-Aug-05 21:22 
GeneralHandle leaks Pin
madkoala30-Jun-05 15:31
sussmadkoala30-Jun-05 15:31 
AnswerRe: Handle leaks Pin
Yves Tkaczyk20-Oct-05 10:47
memberYves Tkaczyk20-Oct-05 10:47 
QuestionPass CMainFrame pointer? Pin
ehh17-Feb-05 5:06
memberehh17-Feb-05 5:06 
AnswerRe: Pass CMainFrame pointer? Pin
Dan Madden8-Jun-05 11:15
memberDan Madden8-Jun-05 11:15 
AnswerRe: Pass CMainFrame pointer? Pin
Blake Miller25-Aug-05 8:11
memberBlake Miller25-Aug-05 8:11 
GeneralRe: Pass CMainFrame pointer? Pin
ehh1-Sep-05 3:36
memberehh1-Sep-05 3:36 
GeneralRe: Pass CMainFrame pointer? Pin
Blake Miller1-Sep-05 3:56
memberBlake Miller1-Sep-05 3:56 
GeneralTimeout Pin
dspeziale12-Dec-04 22:57
memberdspeziale12-Dec-04 22:57 
Generali wnt yours helpe Pin
sultan_saud1-Nov-04 2:55
membersultan_saud1-Nov-04 2:55 
GeneralUse CWinThread instead of ::CreateThread Pin
Andrey Del Pozo6-Sep-04 10:22
memberAndrey Del Pozo6-Sep-04 10:22 
GeneralRe: Use CWinThread instead of ::CreateThread Pin
Blake Miller25-Aug-05 8:05
memberBlake Miller25-Aug-05 8:05 
GeneralIO Completion port / QueueUserWorkItem Pin
Vladislav Gelfer6-Jun-04 10:42
memberVladislav Gelfer6-Jun-04 10:42 
GeneralMemory Problem with AdoConnection Pin
VietDelphi7-May-04 5:08
memberVietDelphi7-May-04 5:08 
GeneralRe: Memory Problem with AdoConnection Pin
Blake Miller25-Aug-05 8:00
memberBlake Miller25-Aug-05 8:00 
Generalmemory leaks Pin
Justin884811-Nov-03 20:44
memberJustin884811-Nov-03 20:44 
GeneralDetected Memory Leaks Pin
Kurt Muellner26-Aug-03 8:32
memberKurt Muellner26-Aug-03 8:32 
GeneralRe: Detected Memory Leaks Pin
Kurt Muellner26-Aug-03 20:25
memberKurt Muellner26-Aug-03 20:25 
QuestionDo IIS At a Core level works same way? Pin
vmalvania23-Jun-03 1:36
membervmalvania23-Jun-03 1:36 
QuestionHow do you know when the WorkQueue has finished? Pin
DJ MACROSS22-May-03 9:33
memberDJ MACROSS22-May-03 9:33 
AnswerRe: How do you know when the WorkQueue has finished? Pin
michael_cowan6-Nov-03 10:49
membermichael_cowan6-Nov-03 10:49 
GeneralRe: How do you know when the WorkQueue has finished? Pin
Daniel Madden6-Nov-03 21:55
memberDaniel Madden6-Nov-03 21:55 
GeneralRe: How do you know when the WorkQueue has finished? Pin
mcpata20027-May-04 18:44
membermcpata20027-May-04 18:44 
GeneralSimple Question Pin
Brad Loyd17-Apr-03 10:26
memberBrad Loyd17-Apr-03 10:26 
GeneralRe: Simple Question Pin
ming100025-Apr-03 8:49
memberming100025-Apr-03 8:49 
GeneralRe: Simple Question Pin
cvmanjesh11-Oct-07 21:08
membercvmanjesh11-Oct-07 21:08 
GeneralThreads keep alive...why? :( Pin
Martin_Viet10-Apr-03 7:20
memberMartin_Viet10-Apr-03 7:20 
GeneralRe: Threads keep alive...why? :( Pin
ming100013-Apr-03 11:59
memberming100013-Apr-03 11:59 
GeneralThanx you very much!!!!!! (;)) Pin
Anonymous25-Apr-03 7:55
sussAnonymous25-Apr-03 7:55 
GeneralMuchas Gracias por tu Código y Saludos desde Argentina ;) Pin
Martin_Viet25-Mar-03 8:41
memberMartin_Viet25-Mar-03 8:41 
GeneralRe: Muchas Gracias por tu Código y Saludos desde Argentina ;) Pin
Anonymous9-Apr-03 13:55
sussAnonymous9-Apr-03 13:55 
GeneralRe: Muchas Gracias por tu C&#243;digo y Saludos desde Argentina ;) Pin
Uri Twig10-Jan-05 4:38
memberUri Twig10-Jan-05 4:38 
QuestionShould prevent inserting work item after destroy? Pin
lanzhu23-Mar-03 20:25
memberlanzhu23-Mar-03 20:25 
AnswerRe: Should prevent inserting work item after destroy? Pin
Uri Twig9-Apr-03 14:02
memberUri Twig9-Apr-03 14:02 
GeneralQuestion about using pThreadDataArray Pin
ming100020-Mar-03 22:07
memberming100020-Mar-03 22:07 
GeneralRe: Question about using pThreadDataArray Pin
lanzhu23-Mar-03 20:02
memberlanzhu23-Mar-03 20:02 
GeneralQuestion about using pThreadDataArray Pin
VietDelphi6-May-03 5:35
memberVietDelphi6-May-03 5:35 
GeneralQueueUserWorkItem Pin
Iulian Iuga10-Feb-03 2:10
memberIulian Iuga10-Feb-03 2:10 
GeneralRe: QueueUserWorkItem Pin
Anonymous14-Feb-03 0:24
sussAnonymous14-Feb-03 0:24 
GeneralRe: QueueUserWorkItem Pin
Uri Twig14-Feb-03 0:29
memberUri Twig14-Feb-03 0:29 
GeneralRe: QueueUserWorkItem Pin
mystro_AKA_kokie15-Jun-03 11:39
membermystro_AKA_kokie15-Jun-03 11:39 
GeneralSome Issues Pin
Brian Shifrin9-Feb-03 0:36
memberBrian Shifrin9-Feb-03 0:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 2 Feb 2003
Article Copyright 2003 by Uri Twig
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid