diff --git a/NPLib/CMakeLists.txt b/NPLib/CMakeLists.txt index dae59391ea14715418ae5877ced3730b23deaf82..89c549239491324d7ab3f46876589151f1ad3104 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 1c8274a717eeafc557b8f2cd7ea756cd99d811c9..0801bc36c3ebde76c61b48969f27717a3e6a07fd 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 3795d645516330e164e3aa58fdd55208b777f4d0..07e280bcc371132daff707f6cfedbc7dbcb63e97 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 769af9518d928da6b99b33801ee94ce84ee26b80..89cdc2f16afb073886f8beac3028d41f61c22fbb 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 8931b942f8b69c94f5fd7794a38096d4c70f727b..29af99a16a000f355c2a2a44c9bac339af3d6311 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