title: How to trace resource deadlocks?
body: <p>I've wrote a timer using std::thread - here is how it looks like:</p>
  
  <pre><code>TestbedTimer::TestbedTimer(char type, void* contextObject) : 
    Timer(type, contextObject) {
      this-&gt;active = false;
  }
  
  TestbedTimer::~TestbedTimer(){
      if (this-&gt;active) {
          this-&gt;active = false;
  
          if(this-&gt;timer-&gt;joinable()){
  
              try {
                  this-&gt;timer-&gt;join();
              } catch (const std::system_error&amp; e) {
                  std::cout &lt;&lt; "Caught system_error with code " &lt;&lt; e.code()  &lt;&lt; 
                                " meaning " &lt;&lt; e.what() &lt;&lt; '\n';
              }
          }
  
         if(timer != nullptr) {
             delete timer;
         }
      }
  }
  
  void TestbedTimer::run(unsigned long timeoutInMicroSeconds){
      this-&gt;active = true;
      timer = new std::thread(&amp;TestbedTimer::sleep, this, timeoutInMicroSeconds);
  }
  
  void TestbedTimer::sleep(unsigned long timeoutInMicroSeconds){
      unsigned long interval = 500000;
  
      if(timeoutInMicroSeconds &lt; interval){
          interval = timeoutInMicroSeconds;
      }
  
      while((timeoutInMicroSeconds &gt; 0) &amp;&amp; (active == true)){
          if (active) {
              timeoutInMicroSeconds -= interval;
              /// set the sleep time
              std::chrono::microseconds duration(interval);
              /// set thread to sleep
              std::this_thread::sleep_for(duration);
          }
      }
  
      if (active) {
         this-&gt;notifyAllListeners();
      } 
  }
  
  void TestbedTimer::interrupt(){
      this-&gt;active = false;
  }
  </code></pre>
  
  <p>I'm not really happy with that kind of implementation since I let the timer sleep for a short interval and check if the active flag has changed (but I don't know a better solution since you can't interrupt a sleep_for call). However, my program core dumps with the following message:</p>
  
  <pre><code>thread is joinable
  Caught system_error with code generic:35 meaning Resource deadlock avoided
  thread has rejoined main scope
  terminate called without an active exception
  Aborted (core dumped)
  </code></pre>
  
  <p>I've looked up this error and as seems that I have a thread which waits for another thread (the reason for the resource deadlock). However, I want to find out where exactly this happens. I'm using a C library (which uses pthreads) in my C++ code which provides among other features an option to run as a daemon and I'm afraid that this interfers with my std::thread code. What's the best way to debug this?</p>
  
  <p>I've tried to use helgrind, but this hasn't helped very much (it doesn't find any error).</p>
  
  <p>TIA</p>
  
  <p>** EDIT: The code above is actually not exemplary code, but I code I've written for a routing daemon. The routing algorithm is a reactive meaning it starts a route discovery only if it has no routes to a desired destination and does not try to build up a routing table for every host in its network. Every time a route discovery is triggered a timer is started. If the timer expires the daemon is notified and the packet is dropped. Basically, it looks like that:</p>
  
  <pre><code>void Client::startNewRouteDiscovery(Packet* packet) {
      AddressPtr destination = packet-&gt;getDestination();
      ...
      startRouteDiscoveryTimer(packet);
      ...
  }
  
  void Client::startRouteDiscoveryTimer(const Packet* packet) {
      RouteDiscoveryInfo* discoveryInfo = new RouteDiscoveryInfo(packet);
      /// create a new timer of a certain type
      Timer* timer = getNewTimer(TimerType::ROUTE_DISCOVERY_TIMER, discoveryInfo);
      /// pass that class as callback object which is notified if the timer expires (class implements a interface for that)
      timer-&gt;addTimeoutListener(this);
      /// start the timer
      timer-&gt;run(routeDiscoveryTimeoutInMilliSeconds * 1000);
  
      AddressPtr destination = packet-&gt;getDestination();
      runningRouteDiscoveries[destination] = timer;
  }
  </code></pre>
  
  <p>If the timer has expired the following method is called.</p>
  
  <pre><code>void Client::timerHasExpired(Timer* responsibleTimer) {
      char timerType = responsibleTimer-&gt;getType();
      switch (timerType) {
          ...
          case TimerType::ROUTE_DISCOVERY_TIMER:
              handleExpiredRouteDiscoveryTimer(responsibleTimer);
              return;
          ....
          default:
              // if this happens its a bug in our code
              logError("Could not identify expired timer");
              delete responsibleTimer;
      }
  }
  </code></pre>
  
  <p>I hope that helps to get a better understanding of what I'm doing. However, I did not to intend to bloat the question with that additional code.</p>