Click here to Skip to main content
11,709,014 members (61,369 online)
Click here to Skip to main content

A simple thread pool class

, 31 Dec 2003 69.9K 1.8K 14
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

digger_chen
Web Developer
United States United States
hello

You may also be interested in...

Comments and Discussions

 
SuggestionAnother way Pin
paolobia4-Dec-14 0:50
memberpaolobia4-Dec-14 0:50 
GeneralA Big Bug! Pin
Mycro6-Jul-10 18:46
memberMycro6-Jul-10 18:46 
GeneralRuntime problems with this sample code. Pin
mark.steddom@gmail.com17-Feb-05 13:35
membermark.steddom@gmail.com17-Feb-05 13:35 
GeneralRe: Runtime problems with this sample code. Pin
monkbiao27-Sep-05 22:17
membermonkbiao27-Sep-05 22:17 
GeneralLetting the threads close nicely. Pin
Merlinblack14-Nov-04 14:19
memberMerlinblack14-Nov-04 14:19 
GeneralRe: Letting the threads close nicely. Pin
Merlinblack14-Nov-04 14:22
memberMerlinblack14-Nov-04 14:22 
GeneralA little bug Pin
Mathieu DEGROUX16-Apr-04 5:01
memberMathieu 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
sussAnonymous2-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
sussAnonymous14-Jan-04 3:25 
GeneralWinAPI on polling Pin
Anonymous2-Jan-04 4:37
sussAnonymous2-Jan-04 4:37 
GeneralRe: WinAPI on polling Pin
Anonymous14-Jan-04 3:28
sussAnonymous14-Jan-04 3:28 
GeneralRe: WinAPI on polling Pin
Blake Miller25-Aug-05 6:30
memberBlake Miller25-Aug-05 6:30 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 1 Jan 2004
Article Copyright 2004 by digger_chen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid