Click here to Skip to main content
Licence CPOL
First Posted 1 Mar 2002
Views 145,245
Downloads 2,442
Bookmarked 27 times

A Thread Pool compatible with Win32 and pthreads API

By | 9 Apr 2008 | Article
A cross-platform thread pooling implementation

Introduction

I had a requirement to create a server that could run on Windows and Linux platforms. One of the basic requirements of the server was an efficient thread-pooling scheme. I came across the pthreads API. I already had a thread pooling scheme that used IO Completion ports. Using a few defines and pre-processor directives, I changed the existing thread pool to support the pthread library. I have NOT compiled this on Linux yet.

Requirements:

It uses the STL containers vector and deque.

It also uses the for_each STL algorithm.
The classes themselves do not use MFC, but the demo app is created using MFC Single Document App-Wizard.
Requires pthreads library on non-Win32 platforms.

To Use

To use the native Win32 API, comment the line:

#include <pthread/pthread.h&>

in the header file 'defines.h'.

Step 1: Include the header file thread.h.

Step 2: Derive a class CMyJob from ThreadJob and implement the 'execute' virtual function. After the job to be done in the thread is complete, do not forget to call the 'execute' function of the base class.

Step 3: In the application's initialization, call

CThread::get_threadpool().start(num);

This creates the threads and waits on them.

Step 4: When you have a job to be queued on the pool,

CMyJob* pjob = new CMyJob
// initialize pjob

CThread::get_threadpool().add_job(pjob);

The thread pool executes the job and after the job is completed, CMyJob deletes itself.

Step 5: When the application quits, call

CThread::get_threadpool().stop();

This prevents any more jobs being queued, deletes pending jobs, stops the threads and then returns.

You can obtain the pthreads library from ftp://sources.redhat.com/pub/pthreads-win32/dll-latest to test with pthreads.

In my tests, pthreads on Win32 was a fraction slower than the Win32. This could be because pthreads on Win32 is a wrapper over the native Win32 threads API.

Please send any suggestions to improve this class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Thomas George



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThere are 2 bugs ? PinmemberI-BIRD6:43 8 Jun '09  
GeneralFor linux compiling ( information ) PinmemberI-BIRD5:13 27 Apr '09  
Generalwhen compiling win vs express 8 geting compilation error Pinmemberumen24211:52 14 Oct '08  
GeneralRe: when compiling win vs express 8 geting compilation error Pinmemberumen2422:41 16 Oct '08  
GeneralTesting PinmvpJeffrey Walton5:19 10 Apr '08  
GeneralRe: Testing PinmemberThomas George5:27 10 Apr '08  
GeneralProblem with g++ > 4.2 Pinmemberleso0320:29 5 Mar '08  
Generalpthread code should use a global mutex on the condition Pinmemberpaul@elphin.com22:56 6 Jan '05  
QuestionWho have used zThread library on win32 platform(VC++ 6.0)? Pinmemberkimy19:53 1 Dec '02  
QuestionDo I have to create a "pthreadVC.lib" folder? PinmemberWREY11:39 18 Jul '02  
AnswerRe: Do I have to create a "pthreadVC.lib" folder? PinmemberThomas George9:23 20 Jul '02  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberWREY10:34 20 Jul '02  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberRicky Chow20:06 9 Mar '03  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberWREY5:08 10 Mar '03  
QuestionDeleted twice!? PinmemberAnonymous5:02 13 May '02  
AnswerRe: Deleted twice!? PinmemberThomas George9:20 20 Jul '02  
GeneralQuestion from my ignorance PinmemberTim Smith5:15 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberThomas George5:24 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith5:44 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberThomas George5:54 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith7:55 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberThomas George8:35 3 Mar '02  
I am not suggesting that PulseEvent is better than SetEvent or is more suitable for any purpose.
 
Let me clarify the condition variable part first. The equivalent of events in pthreads API is called condition variables, meaning that based on the state/condition of that variable, threads wake up.
 
Now coming back to the reason why I mentioned PulseEvent,
 
I was merely stating that the pthread_cond_signal in pthreads library behaves similar to PulseEvent in Win32 API than any other Win32 API call.
 
In the pthreads API, to do the equivalent of
 
CreateEvent
WaitForSingleObject
WaitForMultipleObjects
SetEvent
ResetEvent
PulseEvent
 
etc,...,
 
they have
 
pthread_cond_init - initialize a condition variable
pthread_cond_wait - wait on a condition variable
pthread_cond_signal - signal a condition variable to wake up one thread (this is the only call that can wake up one thread)
pthread_cond_broadcast - wake up all threads waiting on the condition variable.
 
Now,
The pthread_cond_signal does nothing when no threads are waiting on the condition object, similar to the behaviour of PulseEvent, where as SetEvent maintains the event signalled until a thread is available and is released.
 
So, if there has to be one to one mapping of the functions, pthread_cond_signal would be mapped to PulseEvent rather than SetEvent because SetEvent will always cause a thread to be released, where as PulseEvent or pthread_cond_signal does not.
 
Thomas George
GeneralRe: Question from my ignorance PinmemberTim Smith8:56 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberAnonymous10:24 4 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith10:32 4 Mar '02  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 9 Apr 2008
Article Copyright 2002 by Thomas George
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid