icinga2/lib/base/threadpool.cpp

37 lines
584 B
C++
Raw 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;
ThreadPool::ThreadPool(size_t threads)
2019-08-14 11:12:59 -04:00
: m_Threads(threads), 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) {
m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(m_Threads));
}
2013-02-17 13:14:34 -05:00
}
2012-06-23 20:56:48 -04:00
void ThreadPool::Stop()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (m_Pool) {
m_Pool->join();
m_Pool = nullptr;
}
}