From 66dd2739029567acaa43a12cd5cb5d1429f9254e Mon Sep 17 00:00:00 2001
From: Martin Hierholzer <martin.hierholzer@desy.de>
Date: Wed, 22 Jun 2016 16:50:14 +0200
Subject: [PATCH] terminate thread in destructor

---
 include/FanOut.h | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/include/FanOut.h b/include/FanOut.h
index 61671a16..1d851749 100644
--- a/include/FanOut.h
+++ b/include/FanOut.h
@@ -40,6 +40,14 @@ namespace ChimeraTK {
       : impl(nullptr), _direction(VariableDirection::feeding)
       {}
 
+      /** The destructor will terminate the internal thread, if running */
+      ~FanOut() {
+        if(_thread.joinable()) {
+          requestTerminateThread = true;
+          _thread.join();
+        }
+      }
+
       /** Add a slave to the FanOut. Only sending end-points of a consuming node may be added. */
       void addSlave(boost::shared_ptr<mtca4u::ProcessVariable> slave) {
         if(!slave->isSender()) {
@@ -68,6 +76,7 @@ namespace ChimeraTK {
 
       void activate() {
         assert(_direction == VariableDirection::consuming);
+        assert(!_thread.joinable());
         _thread = std::thread([this] { this->run(); });
       }
 
@@ -78,7 +87,10 @@ namespace ChimeraTK {
         while(true) {
           // wait for external trigger
           /// @todo TODO replace with proper blocking implementation when supported by the CSA
-          while(externalTrigger->receive() == false) std::this_thread::yield();
+          while(externalTrigger->receive() == false) {
+            if(requestTerminateThread) return;
+            std::this_thread::yield();
+          }
           // receive data
           impl->receive();
           for(auto &slave : slaves) {     // send out copies to slaves
@@ -157,6 +169,9 @@ namespace ChimeraTK {
       /** Thread handling the synchronisation, if needed */
       std::thread _thread;
 
+      /** Flag to request termination of the synchronisation thread. */
+      bool requestTerminateThread{false};
+
   };
 
 } /* namespace ChimeraTK */
-- 
GitLab