Commit 44d712f3 authored by Tobias WEBER's avatar Tobias WEBER

updated thread pool

parent 9bfdb5cc
......@@ -24,6 +24,7 @@ General Mathematics
* (Arfken 2013), G. B. Arfken, "Mathematical Methods for Physicists", 2013, ISBN: 978-0-12-384654-9.
* (Arens 2015), T. Arens et al., "Mathematik", 2015, ISBN: 978-3-642-44919-2
-------------------------------------------------------------------------------
General Physics
-------------------------------------------------------------------------------
......
......@@ -16,15 +16,17 @@
#include <algorithm>
#include <type_traits>
#include <memory>
#include <boost/asio.hpp>
namespace tl {
/**
* minimalistic thread pool
* wrapper for boost thread pool
*/
template<class t_func> class ThreadPool
template<class t_func>
class ThreadPool
{
public:
using t_ret = typename std::result_of<t_func&()>::type;
......@@ -33,65 +35,28 @@ template<class t_func> class ThreadPool
protected:
std::list<std::unique_ptr<std::thread>> m_lstThreads;
mutable std::mutex m_mtx;
boost::asio::thread_pool m_tp;
std::mutex m_mtx;
// list of wrapped function to be executed
t_task m_lstTasks;
// futures with function return values
t_fut m_lstFutures;
// signal to start jobs
std::promise<void> m_signalStartIn;
std::future<void> m_signalStartOut = std::move(m_signalStartIn.get_future());
void (*m_pThStartFunc)() = nullptr;
public:
ThreadPool(unsigned int iNumThreads = std::thread::hardware_concurrency(),
void (*pThStartFunc)() = nullptr)
{
// start 'iNumThreads' threads
for(unsigned int iThread=0; iThread<iNumThreads; ++iThread)
{
m_lstThreads.emplace_back(
std::unique_ptr<std::thread>(new std::thread([this, pThStartFunc, iThread]()
{
// callback to invoke before starting job thread
if(pThStartFunc) (*pThStartFunc)();
m_signalStartOut.wait();
while(1)
{
std::unique_lock<std::mutex> lock0(m_mtx);
// is a task available
if(m_lstTasks.size() > 0)
{
// pop task from list
std::packaged_task<t_ret()> task =
std::move(m_lstTasks.front());
m_lstTasks.pop_front();
lock0.unlock();
// run task
//tl::log_debug("Thread ", iThread, ": running task.");
task();
}
else
break;
}
})));
}
}
: m_tp{iNumThreads}, m_pThStartFunc{pThStartFunc}
{}
virtual ~ThreadPool()
{
JoinAll();
m_lstThreads.clear();
Join();
}
......@@ -106,35 +71,33 @@ template<class t_func> class ThreadPool
std::lock_guard<std::mutex> lock(m_mtx);
m_lstTasks.emplace_back(std::move(task));
m_lstFutures.emplace_back(std::move(fut));
}
std::packaged_task<t_ret()>* thetask = &m_lstTasks.back();;
/**
* issue start signal
*/
void StartTasks()
{
m_signalStartIn.set_value();
boost::asio::post(m_tp, [this, thetask]() -> void
{
if(m_pThStartFunc)
(*m_pThStartFunc)();
(*thetask)();
});
}
t_fut& GetFutures() { return m_lstFutures; }
t_fut& GetResults() { return m_lstFutures; }
t_task& GetTasks() { return m_lstTasks; }
/**
* wait for all tasks to be finished
*/
void JoinAll()
void Join()
{
std::for_each(m_lstThreads.begin(), m_lstThreads.end(),
[](std::unique_ptr<std::thread>& pThread)
{
if(pThread)
pThread->join();
});
m_tp.join();
}
};
}
#endif
......@@ -184,6 +184,7 @@ Log::Log(const std::string& strInfo, LogColor col, std::ostream* pOstr)
Log::~Log()
{
std::lock_guard<decltype(s_mtx)> _lck(s_mtx);
//std::cerr << "Removing " << m_strInfo << " logger." << std::endl;
m_mapOstrsTh.clear();
m_vecOstrs.clear();
......
......@@ -181,9 +181,8 @@ class Brillouin3D
return std::make_pair(true, planeperp);
});
}
poolMiddlePerps.StartTasks();
for(auto& fut : poolMiddlePerps.GetFutures())
for(auto& fut : poolMiddlePerps.GetResults())
{
auto pair = fut.get();
if(pair.first)
......@@ -219,11 +218,10 @@ class Brillouin3D
}
}
}
poolIntersections.StartTasks();
std::size_t iVertsRemoved = 0;
Rt<T, 3, 8, 0> rt;
for(auto& fut : poolIntersections.GetFutures())
for(auto& fut : poolIntersections.GetResults())
{
auto pair = fut.get();
if(pair.first)
......@@ -274,12 +272,11 @@ class Brillouin3D
return std::make_pair(!bRemoveVertex, iVert);
});
}
poolVerts.StartTasks();
std::vector<t_vec<T>> vecNewVertices;
vecNewVertices.reserve(m_vecVertices.size());
for(auto& fut : poolVerts.GetFutures())
for(auto& fut : poolVerts.GetResults())
{
auto pair = fut.get();
if(pair.first)
......@@ -315,9 +312,8 @@ class Brillouin3D
return std::make_tuple(bOk, vecPoly, plane);
});
}
poolPolys.StartTasks();
for(auto& fut : poolPolys.GetFutures())
for(auto& fut : poolPolys.GetResults())
{
auto tup = fut.get();
if(std::get<0>(tup))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment