5,666,979 members and growing! (17,482 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Threads     Intermediate

Work Queue

By Uri Twig

Simple and elegant thread pool.
VC6, VC7, C++Windows, NT4, Win2K, WinXP, STL, VS6, Visual Studio, Dev

Posted: 1 Feb 2003
Updated: 1 Feb 2003
Views: 132,246
Bookmarked: 82 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
35 votes for this Article.
Popularity: 7.01 Rating: 4.54 out of 5
1 vote, 2.9%
1
0 votes, 0.0%
2
1 vote, 2.9%
3
5 votes, 14.3%
4
28 votes, 80.0%
5

Sample Image - Work_Queue.gif

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

About the Author

Uri Twig


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

Occupation: Web Developer
Location: Israel Israel

Other popular Threads, Processes & IPC articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 62 (Total in Forum: 62) (Refresh)FirstPrevNext
GeneralMessages in the worker threadmemberchocm22:30 14 Oct '08  
General....very nice code!!memberAndrea7522:39 24 Nov '05  
GeneralCancel an expecific threadmemberDesvariovsk7:40 15 Oct '05  
QuestionSend cancel from GUI?memberehh6:57 8 Sep '05  
QuestionRe: Send cancel from GUI?memberehh7:20 8 Sep '05  
GeneralUnzipmemberDanny Maes22:22 11 Aug '05  
GeneralHandle leakssussmadkoala16:31 30 Jun '05  
AnswerRe: Handle leaksmemberYves Tkaczyk11:47 20 Oct '05  
GeneralPass CMainFrame pointer?memberehh6:06 17 Feb '05  
GeneralRe: Pass CMainFrame pointer?memberDan Madden12:15 8 Jun '05  
GeneralRe: Pass CMainFrame pointer?memberBlake Miller9:11 25 Aug '05  
GeneralRe: Pass CMainFrame pointer?memberehh4:36 1 Sep '05  
GeneralRe: Pass CMainFrame pointer?memberBlake Miller4:56 1 Sep '05  
GeneralTimeoutmemberdspeziale23:57 12 Dec '04  
Generali wnt yours helpemembersultan_saud3:55 1 Nov '04  
GeneralUse CWinThread instead of ::CreateThreadmemberAndrey Del Pozo11:22 6 Sep '04  
GeneralRe: Use CWinThread instead of ::CreateThreadmemberBlake Miller9:05 25 Aug '05  
GeneralIO Completion port / QueueUserWorkItemmemberVladislav Gelfer11:42 6 Jun '04  
GeneralMemory Problem with AdoConnectionmemberVietDelphi6:08 7 May '04  
GeneralRe: Memory Problem with AdoConnectionmemberBlake Miller9:00 25 Aug '05  
Generalmemory leaksmemberJustin884821:44 11 Nov '03  
GeneralDetected Memory LeaksmemberKurt Muellner9:32 26 Aug '03  
GeneralRe: Detected Memory LeaksmemberKurt Muellner21:25 26 Aug '03  
GeneralDo IIS At a Core level works same way?membervmalvania2:36 23 Jun '03  
GeneralHow do you know when the WorkQueue has finished?memberDJ MACROSS10:33 22 May '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Feb 2003
Editor: Marc Clifton
Copyright 2003 by Uri Twig
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project