icinga2/base/threadpool.h

65 lines
2.2 KiB
C
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
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);
void WaitForTasks(void);
2012-06-17 20:35:56 +02:00
private:
2012-08-13 13:06:43 +02:00
mutable boost::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 */