Boost logo

Boost :

Subject: [boost] [thread 1.48] Multiple interrupt/timed_join leads to deadlock
From: Gaetano Mendola (mendola_at_[hidden])
Date: 2012-12-04 13:32:18


Hi all,
I was investigating a rare deadlock when issuing an interrupt and
a timed_join in parallel. I come out with the the following code
showing the behavior.

This is the backtrace of it: http://pastebin.com/xArHgHXf

The deadlock is rare so sometime you need to wait a bit.

I couldn't try it with boost 1.52 because the code is invalid
due the precondition of "thread joinable" when issuing the
timed_join.

Is the code not valid or a real bug?

Regards
Gaetano Mendola

////////////////////// CODE //////////////////////
#include <iostream>
#include <boost/thread.hpp>

struct Runner {
   void operator()() {
     const boost::posix_time::time_duration mySeconds =
boost::posix_time::seconds(100);
     boost::this_thread::sleep(mySeconds);
   }
};

struct Interrupter {
   Interrupter(boost::thread& aThread, boost::barrier& aBarrier)
       : theThread(aThread),
         theSeconds(boost::posix_time::seconds(1)),
         theBarrier(aBarrier)
   { }
   void operator()() {
     theBarrier.wait();
     for (int i=0; i<1000;++i) {
       theThread.interrupt();
       theThread.timed_join(theSeconds);
     }
   }
   boost::thread& theThread;
   boost::posix_time::time_duration theSeconds;
   boost::barrier& theBarrier;
};

int main() {

    for (size_t i = 0; i < 1000000; ++i) {
     Runner myRunner;
     boost::thread myRunnerThread(myRunner);

     boost::barrier myBarrier(2);

     Interrupter myInterrupter1(myRunnerThread, myBarrier);
     Interrupter myInterrupter2(myRunnerThread, myBarrier);

     boost::thread myInterrupterThread1(myInterrupter1);
     boost::thread myInterrupterThread2(myInterrupter2);

     myInterrupterThread1.join();
     myInterrupterThread2.join();
     myRunnerThread.join();
     std::cout << "."; std::cout.flush();
  }
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk