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

A simple thread pool class

Rate me:
Please Sign up or sign in to vote.
2.95/5 (15 votes)
31 Dec 2003 86.9K   1.9K   14   12
A simple thread pool class

Introduction

ThreadPool is a very important concept to improve the performance of multi-threaded programs. Generally the threadpool includes an asynchronous queue to keep the tasks that a thread can pick a task from and the caller can put a task into.

In the threadpool code, we define an Asynchronous queue, a Job interface and a ThreadPool class to manage the threads. The synchronization is the core of the threadpool.

I have implemented some classes that will make it easier for you.

Details

It is very easy to use the threadpool class. First you should implement your job class by inheriting the "Job" interface. That means you need to implement two interface functions.

class Job {
public:
    virtual void Process() = 0;
    virtual ~Job() {};
};

For example, you define your job class as

class MyJob1 : public Job {
public:
    MyJob1(int index) : data(index) {};
    virtual void Process() {
        cout << data << endl;
    }
    ~MyJob1() {};

private:
    int data;
};

The "Process" is called by the thread. Then you can use the threadpool as following.

ThreadPool tp(4, TRUE);
Job *job = new MyJob1(0);
tp.Assignment(job);

Of course I intentionally made it simple for you to understand how to create and use threadpool. You can freely modify it and add other features.

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


Written By
Web Developer
United States United States
hello

Comments and Discussions

 
SuggestionAnother way Pin
paolobia4-Dec-14 0:50
paolobia4-Dec-14 0:50 
GeneralA Big Bug! Pin
Mycro6-Jul-10 18:46
Mycro6-Jul-10 18:46 
GeneralRuntime problems with this sample code. Pin
mark.steddom@gmail.com17-Feb-05 13:35
mark.steddom@gmail.com17-Feb-05 13:35 
GeneralRe: Runtime problems with this sample code. Pin
monkbiao27-Sep-05 22:17
monkbiao27-Sep-05 22:17 
GeneralLetting the threads close nicely. Pin
Nigel Atkinson14-Nov-04 14:19
Nigel Atkinson14-Nov-04 14:19 
GeneralRe: Letting the threads close nicely. Pin
Nigel Atkinson14-Nov-04 14:22
Nigel Atkinson14-Nov-04 14:22 
GeneralA little bug Pin
Mathieu DEGROUX16-Apr-04 5:01
Mathieu DEGROUX16-Apr-04 5:01 
GeneralWhat's inside --- the 100lines code (roughly) contains STL based queue, using some event/mutext to protect race condition Pin
Anonymous2-Jan-04 5:02
Anonymous2-Jan-04 5:02 
GeneralRe: What's inside --- the 100lines code (roughly) contains STL based queue, using some event/mutext to protect race condition Pin
Anonymous14-Jan-04 3:25
Anonymous14-Jan-04 3:25 
GeneralWinAPI on polling Pin
Anonymous2-Jan-04 4:37
Anonymous2-Jan-04 4:37 
GeneralRe: WinAPI on polling Pin
Anonymous14-Jan-04 3:28
Anonymous14-Jan-04 3:28 
GeneralRe: WinAPI on polling Pin
Blake Miller25-Aug-05 6:30
Blake Miller25-Aug-05 6:30 

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.