Restart thread pool after freezing Configuration

The user (-D) or we could have changed Configuration.Concurrency,
so correct the thread pool's thread amount.
This commit is contained in:
Alexander A. Klimov 2023-05-23 14:41:35 +02:00
parent 32eb1680f7
commit 3fae41ef22
3 changed files with 22 additions and 1 deletions

View File

@ -451,6 +451,8 @@ static int Main()
Configuration::Concurrency = std::thread::hardware_concurrency();
}
Application::GetTP().Restart();
/* Ensure that all defined constants work in the way we expect them. */
HandleLegacyDefines();

View File

@ -20,10 +20,15 @@ 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(Configuration::Concurrency * 2u));
InitializePool();
}
}
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);
@ -33,3 +38,14 @@ void ThreadPool::Stop()
m_Pool = nullptr;
}
}
void ThreadPool::Restart()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
if (m_Pool) {
m_Pool->join();
}
InitializePool();
}

View File

@ -42,6 +42,7 @@ public:
void Start();
void Stop();
void Restart();
/**
* Appends a work item to the work queue. Work items will be processed in FIFO order.
@ -91,6 +92,8 @@ private:
boost::shared_mutex m_Mutex;
std::unique_ptr<boost::asio::thread_pool> m_Pool;
Atomic<uint_fast64_t> m_Pending;
void InitializePool();
};
}