2012-06-17 20:35:56 +02:00
|
|
|
#ifndef THREADPOOL_H
|
|
|
|
#define THREADPOOL_H
|
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2012-06-19 12:23:52 +02:00
|
|
|
struct ThreadPoolTask
|
|
|
|
{
|
|
|
|
typedef shared_ptr<ThreadPoolTask> Ptr;
|
|
|
|
typedef weak_ptr<ThreadPoolTask> WeakPtr;
|
|
|
|
|
|
|
|
virtual void Execute(void) = 0;
|
|
|
|
};
|
|
|
|
|
2012-06-17 20:35:56 +02:00
|
|
|
class I2_BASE_API ThreadPool : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef shared_ptr<ThreadPool> Ptr;
|
|
|
|
typedef weak_ptr<ThreadPool> WeakPtr;
|
|
|
|
|
2012-06-20 10:46:18 +02:00
|
|
|
ThreadPool(long maxThreads = 128);
|
2012-06-17 20:35:56 +02:00
|
|
|
~ThreadPool(void);
|
|
|
|
|
|
|
|
static ThreadPool::Ptr GetDefaultPool(void);
|
|
|
|
|
2012-06-19 12:23:52 +02:00
|
|
|
void EnqueueTasks(list<ThreadPoolTask::Ptr>& tasks);
|
|
|
|
void EnqueueTask(const ThreadPoolTask::Ptr& task);
|
2012-06-18 06:44:26 +02:00
|
|
|
void WaitForTasks(void);
|
2012-06-17 20:35:56 +02:00
|
|
|
|
|
|
|
private:
|
2012-06-19 12:23:52 +02:00
|
|
|
mutable mutex m_Lock;
|
2012-06-17 20:35:56 +02:00
|
|
|
condition_variable m_CV;
|
|
|
|
|
2012-06-19 12:23:52 +02:00
|
|
|
list<ThreadPoolTask::Ptr> m_Tasks;
|
2012-06-17 20:35:56 +02:00
|
|
|
|
|
|
|
thread_group m_Threads;
|
|
|
|
bool m_Alive;
|
|
|
|
|
2012-06-19 12:23:52 +02:00
|
|
|
ThreadPoolTask::Ptr DequeueTask(void);
|
2012-06-17 20:35:56 +02:00
|
|
|
void WorkerThreadProc(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-18 01:29:02 +02:00
|
|
|
#endif /* THREADPOOL_H */
|