icinga2/lib/base/threadpool.cpp

52 lines
773 B
C++
Raw Permalink Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2012-06-23 20:56:48 -04:00
2014-05-25 10:23:35 -04:00
#include "base/threadpool.hpp"
#include <boost/thread/locks.hpp>
2012-06-23 20:56:48 -04:00
using namespace icinga;
2023-01-27 10:34:11 -05:00
ThreadPool::ThreadPool() : m_Pending(0)
2013-02-18 08:40:24 -05:00
{
Start();
2013-02-18 08:40:24 -05:00
}
2012-07-13 17:33:30 -04:00
ThreadPool::~ThreadPool()
2013-02-15 00:47:26 -05:00
{
2013-02-17 13:14:34 -05:00
Stop();
}
void ThreadPool::Start()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (!m_Pool) {
InitializePool();
}
2013-02-17 13:14:34 -05:00
}
2012-06-23 20:56:48 -04:00
void ThreadPool::InitializePool()
{
m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(Configuration::Concurrency * 2u));
}
void ThreadPool::Stop()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (m_Pool) {
m_Pool->join();
m_Pool = nullptr;
}
}
void ThreadPool::Restart()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (m_Pool) {
m_Pool->join();
}
InitializePool();
}