1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-06-17 23:07:05 +01:00

Thread pool implementation

This commit is contained in:
2021-11-28 23:26:17 +00:00
parent 75485219d8
commit 5f192ad30f
6 changed files with 202 additions and 1 deletions

109
lib/Core/ThreadPool.cpp Normal file
View File

@ -0,0 +1,109 @@
/*
* ThreadPool.cpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2021 Antonin Portelli
*
* LatAnalyze 3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#include <LatAnalyze/Core/ThreadPool.hpp>
#include <LatAnalyze/includes.hpp>
using namespace std;
using namespace Latan;
/******************************************************************************
* ThreadPool implementation *
******************************************************************************/
// constructors ////////////////////////////////////////////////////////////////
ThreadPool::ThreadPool(void)
: ThreadPool(std::thread::hardware_concurrency())
{}
ThreadPool::ThreadPool(const unsigned int nThreads)
: nThreads_(nThreads)
{
for (unsigned int t = 0; t < nThreads_; ++t)
{
threads_.push_back(thread(&ThreadPool::workerLoop, this));
}
}
// destructor //////////////////////////////////////////////////////////////////
ThreadPool::~ThreadPool(void)
{
terminate();
}
// get the number of threads ///////////////////////////////////////////////////
unsigned int ThreadPool::getThreadNum(void) const
{
return nThreads_;
}
// get the pool mutex for synchronisation //////////////////////////////////////
std::mutex & ThreadPool::getMutex(void)
{
return mutex_;
}
// worker loop /////////////////////////////////////////////////////////////////
void ThreadPool::workerLoop(void)
{
while (true)
{
Job job;
{
unique_lock<mutex> lock(mutex_);
condition_.wait(lock, [this](){
return !queue_.empty() || terminatePool_;
});
if (terminatePool_ and queue_.empty())
{
return;
}
job = queue_.front();
queue_.pop();
}
job();
}
}
// add jobs ////////////////////////////////////////////////////////////////////
void ThreadPool::addJob(Job newJob)
{
{
unique_lock<mutex> lock(mutex_);
queue_.push(newJob);
}
condition_.notify_one();
}
// wait for completion /////////////////////////////////////////////////////////
void ThreadPool::terminate(void)
{
{
unique_lock<mutex> lock(mutex_);
terminatePool_ = true;
}
condition_.notify_all();
for (auto &thread: threads_)
{
thread.join();
}
threads_.clear();
}

54
lib/Core/ThreadPool.hpp Normal file
View File

@ -0,0 +1,54 @@
/*
* ThreadPool.hpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2021 Antonin Portelli
*
* LatAnalyze 3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Latan_ThreadPool_hpp_
#define Latan_ThreadPool_hpp_
#include <LatAnalyze/Global.hpp>
class ThreadPool
{
public:
typedef std::function<void(void)> Job;
public:
// constructors/destructor
ThreadPool(void);
ThreadPool(const unsigned int nThreads);
virtual ~ThreadPool(void);
// get the number of threads
unsigned int getThreadNum(void) const;
// get the pool mutex for synchronisation
std::mutex & getMutex(void);
// add jobs
void addJob(Job newJob);
// wait for completion and terminate
void terminate(void);
private:
// worker loop
void workerLoop(void);
private:
unsigned int nThreads_;
std::condition_variable condition_;
std::vector<std::thread> threads_;
bool terminatePool_{false};
std::queue<Job> queue_;
std::mutex mutex_;
};
#endif

View File

@ -24,6 +24,7 @@
#include <array>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <fstream>
#include <functional>
#include <iostream>
@ -40,6 +41,7 @@
#include <stack>
#include <string>
#include <sstream>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <utility>