]> granicus.if.org Git - icinga2/blob - lib/base/threadpool.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / threadpool.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/threadpool.hpp"
4 #include <boost/thread/locks.hpp>
5
6 using namespace icinga;
7
8 ThreadPool::ThreadPool(size_t threads)
9         : m_Threads(threads)
10 {
11         Start();
12 }
13
14 ThreadPool::~ThreadPool()
15 {
16         Stop();
17 }
18
19 void ThreadPool::Start()
20 {
21         boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
22
23         if (!m_Pool) {
24                 m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(m_Threads));
25         }
26 }
27
28 void ThreadPool::Stop()
29 {
30         boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
31
32         if (m_Pool) {
33                 m_Pool->join();
34                 m_Pool = nullptr;
35         }
36 }