Click here to Skip to main content
Licence CPOL
First Posted 1 Mar 2002
Views 145,238
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  
I noticed in the job processing code that in the case of pthreads you loop until the job queue is empty. With W32, you only process one.
 
Why the difference?
 
Tim Smith
 
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
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  
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