Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / C++
Article

A Thread Pool compatible with Win32 and pthreads API

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
9 Apr 2008CPOL2 min read 196.1K   3.1K   31   29
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:

C++
#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

C++
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,

C++
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

C++
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)



Comments and Discussions

 
QuestionThere are 2 bugs ? Pin
I-BIRD8-Jun-09 6:43
I-BIRD8-Jun-09 6:43 
GeneralFor linux compiling ( information ) Pin
I-BIRD27-Apr-09 5:13
I-BIRD27-Apr-09 5:13 
Generalwhen compiling win vs express 8 geting compilation error Pin
umen24214-Oct-08 11:52
umen24214-Oct-08 11:52 
GeneralRe: when compiling win vs express 8 geting compilation error Pin
umen24216-Oct-08 2:41
umen24216-Oct-08 2:41 
GeneralTesting Pin
Jeffrey Walton10-Apr-08 5:19
Jeffrey Walton10-Apr-08 5:19 
GeneralRe: Testing Pin
User 988510-Apr-08 5:27
User 988510-Apr-08 5:27 
GeneralProblem with g++ &gt; 4.2 Pin
leso035-Mar-08 20:29
leso035-Mar-08 20:29 
Generalpthread code should use a global mutex on the condition Pin
Member 34166-Jan-05 22:56
Member 34166-Jan-05 22:56 
QuestionWho have used zThread library on win32 platform(VC++ 6.0)? Pin
kimy1-Dec-02 19:53
kimy1-Dec-02 19:53 
QuestionDo I have to create a "pthreadVC.lib" folder? Pin
WREY18-Jul-02 11:39
WREY18-Jul-02 11:39 
AnswerRe: Do I have to create a "pthreadVC.lib" folder? Pin
User 988520-Jul-02 9:23
User 988520-Jul-02 9:23 
GeneralRe: Do I have to create a "pthreadVC.lib" folder? Pin
WREY20-Jul-02 10:34
WREY20-Jul-02 10:34 
GeneralRe: Do I have to create a "pthreadVC.lib" folder? Pin
Ricky Chow9-Mar-03 20:06
Ricky Chow9-Mar-03 20:06 
GeneralRe: Do I have to create a "pthreadVC.lib" folder? Pin
WREY10-Mar-03 5:08
WREY10-Mar-03 5:08 
QuestionDeleted twice!? Pin
13-May-02 5:02
suss13-May-02 5:02 
AnswerRe: Deleted twice!? Pin
User 988520-Jul-02 9:20
User 988520-Jul-02 9:20 
GeneralQuestion from my ignorance Pin
Tim Smith3-Mar-02 5:15
Tim Smith3-Mar-02 5:15 
GeneralRe: Question from my ignorance Pin
User 98853-Mar-02 5:24
User 98853-Mar-02 5:24 
GeneralRe: Question from my ignorance Pin
Tim Smith3-Mar-02 5:44
Tim Smith3-Mar-02 5:44 
GeneralRe: Question from my ignorance Pin
User 98853-Mar-02 5:54
User 98853-Mar-02 5:54 
GeneralRe: Question from my ignorance Pin
Tim Smith3-Mar-02 7:55
Tim Smith3-Mar-02 7:55 
GeneralRe: Question from my ignorance Pin
User 98853-Mar-02 8:35
User 98853-Mar-02 8:35 
GeneralRe: Question from my ignorance Pin
Tim Smith3-Mar-02 8:56
Tim Smith3-Mar-02 8:56 
GeneralRe: Question from my ignorance Pin
4-Mar-02 10:24
suss4-Mar-02 10:24 
GeneralRe: Question from my ignorance Pin
Tim Smith4-Mar-02 10:32
Tim Smith4-Mar-02 10:32 

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

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