Click here to Skip to main content
Click here to Skip to main content

A simple thread pool class

, 31 Dec 2003
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

About the Author

digger_chen
Web Developer
United States United States
hello

Comments and Discussions

 
GeneralA Big Bug! PinmemberMycro6-Jul-10 18:46 
GeneralRuntime problems with this sample code. Pinmembermark.steddom@gmail.com17-Feb-05 13:35 
GeneralRe: Runtime problems with this sample code. Pinmembermonkbiao27-Sep-05 22:17 
GeneralLetting the threads close nicely. PinmemberMerlinblack14-Nov-04 14:19 
GeneralRe: Letting the threads close nicely. PinmemberMerlinblack14-Nov-04 14:22 
GeneralA little bug PinmemberMathieu DEGROUX16-Apr-04 5:01 
GeneralWhat's inside --- the 100lines code (roughly) contains STL based queue, using some event/mutext to protect race condition PinsussAnonymous2-Jan-04 5:02 
GeneralRe: What's inside --- the 100lines code (roughly) contains STL based queue, using some event/mutext to protect race condition PinsussAnonymous14-Jan-04 3:25 
GeneralWinAPI on polling PinsussAnonymous2-Jan-04 4:37 
GeneralRe: WinAPI on polling PinsussAnonymous14-Jan-04 3:28 
GeneralRe: WinAPI on polling PinmemberBlake 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 | Mobile
Web01 | 2.8.140709.1 | Last Updated 1 Jan 2004
Article Copyright 2004 by digger_chen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid