From 7d7b51b1ea4b88c93cd918980913780a28840870 Mon Sep 17 00:00:00 2001 From: Adrien Matta <matta@lpccaen.in2p3.fr> Date: Wed, 28 Apr 2021 11:20:07 +0200 Subject: [PATCH] * improving multithreading --- NPLib/CMakeLists.txt | 2 +- NPLib/Core/NPDetectorManager.cxx | 6 +++++- NPLib/Core/NPDetectorManager.h | 2 ++ NPLib/TrackReconstruction/NPDCReconstructionMT.cxx | 7 +++++-- NPLib/TrackReconstruction/NPDCReconstructionMT.h | 2 ++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/NPLib/CMakeLists.txt b/NPLib/CMakeLists.txt index dae59391e..89c549239 100644 --- a/NPLib/CMakeLists.txt +++ b/NPLib/CMakeLists.txt @@ -25,7 +25,7 @@ if(NOT DEFINED NPMULTITHREADING) endif() if(NPMULTITHREADING) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNPMULTITHREADING=1") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wthread-safety -DNPMULTITHREADING=1") message("Building application with MultiThreading Support") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNPMULTITHREADING=0") diff --git a/NPLib/Core/NPDetectorManager.cxx b/NPLib/Core/NPDetectorManager.cxx index 1c8274a71..0801bc36c 100644 --- a/NPLib/Core/NPDetectorManager.cxx +++ b/NPLib/Core/NPDetectorManager.cxx @@ -249,7 +249,9 @@ void NPL::DetectorManager::ReadConfigurationFile(std::string Path) { void NPL::DetectorManager::BuildPhysicalEvent(){ #if __cplusplus > 199711L && NPMULTITHREADING // add new job + m_mtx.lock(); m_Ready.flip(); + m_mtx.unlock(); std::this_thread::yield(); while(!IsDone()){ @@ -411,7 +413,7 @@ void NPL::DetectorManager::InitThreadPool(){ //////////////////////////////////////////////////////////////////////////////// void NPL::DetectorManager::StartThread(NPL::VDetector* det,unsigned int id){ // Let the main thread start - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + //std::this_thread::sleep_for(std::chrono::milliseconds(100)); while(true){ // Do the job if possible if(m_Ready[id]){ @@ -425,7 +427,9 @@ void NPL::DetectorManager::StartThread(NPL::VDetector* det,unsigned int id){ if(m_CheckSpectra) (det->*m_CheckSpectra)(); } + m_mtx.lock(); m_Ready[id].flip(); + m_mtx.unlock(); std::this_thread::yield(); } else{ diff --git a/NPLib/Core/NPDetectorManager.h b/NPLib/Core/NPDetectorManager.h index 3795d6455..07e280bcc 100644 --- a/NPLib/Core/NPDetectorManager.h +++ b/NPLib/Core/NPDetectorManager.h @@ -30,6 +30,7 @@ #include <string> #include <map> #include <vector> +#include <mutex> #include <iostream> #if __cplusplus > 199711L #include <thread> @@ -82,6 +83,7 @@ namespace NPL{ private: // Thread Pool defined if C++11 is available std::vector<std::thread> m_ThreadPool; std::vector<bool> m_Ready; + std::mutex m_mtx; bool m_stop; public: // Init the Thread Pool diff --git a/NPLib/TrackReconstruction/NPDCReconstructionMT.cxx b/NPLib/TrackReconstruction/NPDCReconstructionMT.cxx index 769af9518..89cdc2f16 100644 --- a/NPLib/TrackReconstruction/NPDCReconstructionMT.cxx +++ b/NPLib/TrackReconstruction/NPDCReconstructionMT.cxx @@ -27,7 +27,6 @@ #include "TGraph.h" #include "TVector3.h" #include "TROOT.h" - using namespace std; using namespace NPL; @@ -222,13 +221,15 @@ void DCReconstructionMT::StartThread(unsigned int id){ // each threads needs its own or the minisation is not thread safe ROOT::Math::Functor* func= new ROOT::Math::Functor(this,&NPL::DCReconstructionMT::SumD,3); //Create the minimiser (deleted by the thread) + m_mtx.lock(); ROOT::Math::Minimizer* mini=ROOT::Math::Factory::CreateMinimizer("Minuit2", "Migrad"); + m_mtx.unlock(); mini->SetFunction(*func); mini->SetPrintLevel(0); // Let the main thread start - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + //std::this_thread::sleep_for(std::chrono::milliseconds(500)); while(true){ // Do the job if possible if(m_Ready[id]){ @@ -254,7 +255,9 @@ void DCReconstructionMT::StartThread(unsigned int id){ m_X100[uid]=(100-m_b[uid])/m_a[uid]; m_minimum[uid] = mini->MinValue(); // notify main thread job is done + m_mtx.lock();// make sure no other thread is reading/writing to the map m_Ready[id].flip(); + m_mtx.unlock(); // Let other thread move up in the queu std::this_thread::yield(); } diff --git a/NPLib/TrackReconstruction/NPDCReconstructionMT.h b/NPLib/TrackReconstruction/NPDCReconstructionMT.h index 8931b942f..29af99a16 100644 --- a/NPLib/TrackReconstruction/NPDCReconstructionMT.h +++ b/NPLib/TrackReconstruction/NPDCReconstructionMT.h @@ -40,6 +40,7 @@ #include <map> #include <vector> #include <thread> +#include <mutex> class TVector3; class TGraph; namespace NPL{ @@ -98,6 +99,7 @@ namespace NPL{ std::vector<std::thread> m_ThreadPool; std::vector<bool> m_Ready; bool m_stop; + std::mutex m_mtx; public: // Init the Thread Pool -- GitLab