mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2025-04-11 03:20:46 +01:00
Thread pool implementation
This commit is contained in:
parent
75485219d8
commit
5f192ad30f
@ -19,7 +19,8 @@ noinst_PROGRAMS = \
|
|||||||
exPlot \
|
exPlot \
|
||||||
exPValue \
|
exPValue \
|
||||||
exRand \
|
exRand \
|
||||||
exRootFinder
|
exRootFinder \
|
||||||
|
exThreadPool
|
||||||
|
|
||||||
exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
|
exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
|
||||||
exCompiledDoubleFunction_CXXFLAGS = $(COM_CXXFLAGS)
|
exCompiledDoubleFunction_CXXFLAGS = $(COM_CXXFLAGS)
|
||||||
@ -73,4 +74,8 @@ exRootFinder_SOURCES = exRootFinder.cpp
|
|||||||
exRootFinder_CXXFLAGS = $(COM_CXXFLAGS)
|
exRootFinder_CXXFLAGS = $(COM_CXXFLAGS)
|
||||||
exRootFinder_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
exRootFinder_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
||||||
|
|
||||||
|
exThreadPool_SOURCES = exThreadPool.cpp
|
||||||
|
exThreadPool_CXXFLAGS = $(COM_CXXFLAGS)
|
||||||
|
exThreadPool_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
||||||
|
|
||||||
ACLOCAL_AMFLAGS = -I .buildutils/m4
|
ACLOCAL_AMFLAGS = -I .buildutils/m4
|
||||||
|
29
examples/exThreadPool.cpp
Normal file
29
examples/exThreadPool.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <LatAnalyze/Core/ThreadPool.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace Latan;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
ThreadPool pool;
|
||||||
|
|
||||||
|
cout << "Using " << pool.getThreadNum() << " threads" << endl;
|
||||||
|
for (unsigned int i = 1; i <= 20; ++i)
|
||||||
|
{
|
||||||
|
pool.addJob([i, &pool](void)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
unique_lock<mutex> lock(pool.getMutex());
|
||||||
|
cout << "job " << i << " wait for " << i*100 << " ms" << endl;
|
||||||
|
}
|
||||||
|
this_thread::sleep_for(chrono::milliseconds(i*100));
|
||||||
|
{
|
||||||
|
unique_lock<mutex> lock(pool.getMutex());
|
||||||
|
cout << "job " << i << " done" << endl;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pool.terminate();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
109
lib/Core/ThreadPool.cpp
Normal file
109
lib/Core/ThreadPool.cpp
Normal 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
54
lib/Core/ThreadPool.hpp
Normal 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
|
@ -24,6 +24,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <complex>
|
#include <complex>
|
||||||
|
#include <condition_variable>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -40,6 +41,7 @@
|
|||||||
#include <stack>
|
#include <stack>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <thread>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
@ -32,6 +32,7 @@ libLatAnalyze_la_SOURCES = \
|
|||||||
Core/MathParser.ypp \
|
Core/MathParser.ypp \
|
||||||
Core/OptParser.cpp \
|
Core/OptParser.cpp \
|
||||||
Core/Plot.cpp \
|
Core/Plot.cpp \
|
||||||
|
Core/ThreadPool.cpp \
|
||||||
Core/Utilities.cpp \
|
Core/Utilities.cpp \
|
||||||
Functional/CompiledFunction.cpp \
|
Functional/CompiledFunction.cpp \
|
||||||
Functional/CompiledModel.cpp \
|
Functional/CompiledModel.cpp \
|
||||||
@ -75,6 +76,7 @@ HPPFILES = \
|
|||||||
Core/OptParser.hpp \
|
Core/OptParser.hpp \
|
||||||
Core/ParserState.hpp \
|
Core/ParserState.hpp \
|
||||||
Core/Plot.hpp \
|
Core/Plot.hpp \
|
||||||
|
Core/ThreadPool.hpp \
|
||||||
Core/stdincludes.hpp \
|
Core/stdincludes.hpp \
|
||||||
Core/Utilities.hpp \
|
Core/Utilities.hpp \
|
||||||
Functional/CompiledFunction.hpp \
|
Functional/CompiledFunction.hpp \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user