[转]使用内存信号灯实现C++线程池
implement thread pool by semapore
/********************** thread pool interface ******************/
/**
* File : SemThreadPool.h
* Desc :
* Author : daizongh
* Version : V1.0
* Date : 2007-09-21
*/
#ifndef _D_TOOLS_THREAD_POOL_H_070921_
#define _D_TOOLS_THREAD_POOL_H_070921_
#include <pthread.h>
#include <semaphore.h>
#include <vector>
#include "Runnable.h"
#ifndef NULL
#define NULL 0
#endif //NULL
namespace d_tools
{
class ThreadPoolC
{
public:
/**
* constructor. construct a thread pool with DEFAULT_THREAD_COUNT threads
*/
ThreadPoolC();
/**
* constractor. construct a thread pool with size threads
*
* @param size the size of thread pool, must be a positive number, and it
* must be lower to MAX_THREAD_COUNT, it will be reset as
* DEFAULT_THREAD_COUNT if it is not between 1 and MAX_THREAD_COUNT
*/
ThreadPoolC(unsigned int size);
/**
* destructs the thread pool object. this will wait until all thread exit
*
* @see ThreadPoolC::wait()
*/
virtual ~ThreadPoolC();
private:
/**
* close the interface of copy constructor. no implement
*/
ThreadPoolC(const ThreadPoolC &);
/**
* close the interface of assign constructor. no implement
*/
ThreadPoolC & operator=(const ThreadPoolC &);
public:
/**
* add a process handle to the thread pool. this will call runner->run()
* function, the reference or runner must be an implement of RunnableC which
* implements function RunnableC::run()
* waitRunner() will pop the last execute runner, noticeably, this will post a
* semaphore for waitRunner(), otherwise, waitRunner() will wait for the semaphore from
* execute()
*
* @param runner the handler of the thread processing, it must be an implement
* of RunnableC which implements function RunnableC::run()
* @see RunnableC::run()
* @see ThreadPoolC::waitRunner()
*/
void execute(RunnableC * runner);
/**
* close this thread pool. after this call, thread pool will stop after all
* thread handler finished
*/
void shutdown();
/**
* close all handler forcibly. after this call, thread pool will stop immediately
* it will call pthread_cancel() with all threads in the thread pool
* this usually will be called when error occurs
*/
void abort();
/**
* pop the last execute runner. this will block the calling thread if there is no
* semaphore, and it will be notified by execute() calling
* this will called in thread function only, do not call waitRunner() in ur program
*
* @return the runner handler
* @see ThreadPoolC::execute(Runnable *)
*/
RunnableC * waitRunner();
/**
* wait util all threads in the pool finished, this calling will block until shutdown()
* or abort() called
*
* @see ThreadPoolC::shutdown()
* @see ThreadPoolC::abort()
*/
void wait();
private:
void start();
public:
/**
* get the default thread pool size
*
* @return the default thread pool size
*/
static unsigned int getDefaultThreadCount() { return DEFAULT_THREAD_COUNT; }
/**
* set the default thread pool size
*
* @param size the default thread pool size, must be positive number
*/
static void setDefaultThreadCount(unsigned int size) { if( size > 0 ) DEFAULT_THREAD_COUNT = size; }
/**
* get the max thread pool size
*
* @return the max thread pool size
*/
static unsigned int getMaxThreadCount() { return MAX_THREAD_COUNT; }
/**
* set the max thread pool size
*
* @param size max thread pool size, must be positive number
*/
static void setMaxThreadCount(unsigned int size) { MAX_THREAD_COUNT = size; }
protected:
pthread_t * m_thrs_id; //all thread id list
unsigned int m_thr_cnt; //the count of valid threads
sem_t m_sem_consume; //the semaphore for thread notify
pthread_mutex_t m_pr_lock; //lock of thread runner handles
std::vector<RunnableC *> m_pr; //thread runner handlers
static unsigned int DEFAULT_THREAD_COUNT; //default thread size
static unsigned int MAX_THREAD_COUNT; //max thread size
};
}
#endif //_D_TOOLS_THREAD_POOL_H_070921_