icinga2/lib/base/threadpool.cpp

37 lines
584 B
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2012-06-24 02:56:48 +02:00
2014-05-25 16:23:35 +02:00
#include "base/threadpool.hpp"
#include <boost/thread/locks.hpp>
2012-06-24 02:56:48 +02:00
using namespace icinga;
ThreadPool::ThreadPool(size_t threads)
2019-08-14 17:12:59 +02:00
: m_Threads(threads), m_Pending(0)
2013-02-18 14:40:24 +01:00
{
Start();
2013-02-18 14:40:24 +01:00
}
2012-07-13 23:33:30 +02:00
ThreadPool::~ThreadPool()
2013-02-15 06:47:26 +01:00
{
2013-02-17 19:14:34 +01:00
Stop();
}
void ThreadPool::Start()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (!m_Pool) {
m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(m_Threads));
}
2013-02-17 19:14:34 +01:00
}
2012-06-24 02:56:48 +02:00
void ThreadPool::Stop()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (m_Pool) {
m_Pool->join();
m_Pool = nullptr;
}
}