Click here to Skip to main content
6,594,932 members and growing! (16,065 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Threads     Intermediate License: The Code Project Open License (CPOL)

A Thread Pool compatible with Win32 and pthreads API

By Thomas George

A cross-platform thread pooling implementation
VC6Win2K, WinXP, STL, Dev
Posted:1 Mar 2002
Updated:9 Apr 2008
Views:116,653
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.35 Rating: 3.10 out of 5
2 votes, 33.3%
1
1 vote, 16.7%
2

3

4
3 votes, 50.0%
5

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


Member

Location: India India

Other popular Threads, Processes & IPC articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
GeneralThere are 2 bugs ? PinmemberI-BIRD7:43 8 Jun '09  
GeneralFor linux compiling ( information ) PinmemberI-BIRD6:13 27 Apr '09  
Generalwhen compiling win vs express 8 geting compilation error Pinmemberumen24212:52 14 Oct '08  
GeneralRe: when compiling win vs express 8 geting compilation error Pinmemberumen2423:41 16 Oct '08  
GeneralTesting PinmvpJeffrey Walton6:19 10 Apr '08  
GeneralRe: Testing PinsupporterThomas George6:27 10 Apr '08  
GeneralProblem with g++ > 4.2 Pinmemberleso0321:29 5 Mar '08  
Generalpthread code should use a global mutex on the condition Pinmemberpaul@elphin.com23:56 6 Jan '05  
GeneralWho have used zThread library on win32 platform(VC++ 6.0)? Pinmemberkimy20:53 1 Dec '02  
GeneralDo I have to create a "pthreadVC.lib" folder? PinmemberWREY12:39 18 Jul '02  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberThomas George10:23 20 Jul '02  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberWREY11:34 20 Jul '02  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberRicky Chow21:06 9 Mar '03  
GeneralRe: Do I have to create a "pthreadVC.lib" folder? PinmemberWREY6:08 10 Mar '03  
GeneralDeleted twice!? PinmemberAnonymous6:02 13 May '02  
GeneralRe: Deleted twice!? PinmemberThomas George10:20 20 Jul '02  
GeneralQuestion from my ignorance PinmemberTim Smith6:15 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberThomas George6:24 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith6:44 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberThomas George6:54 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith8:55 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberThomas George9:35 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith9:56 3 Mar '02  
GeneralRe: Question from my ignorance PinmemberAnonymous11:24 4 Mar '02  
GeneralRe: Question from my ignorance PinmemberTim Smith11:32 4 Mar '02  

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

PermaLink | Privacy | Terms of Use
Last Updated: 9 Apr 2008
Editor: Sean Ewington
Copyright 2002 by Thomas George
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project