3
votes

I have a program that uses boost threads. The program has start and stop functionality. When the program is started I create a boost thread that does some processing. When the program is stopped I call join on this thread and delete the thread's pointer. My program starts and stops correctly the first time; however, when I try to start my program a second time I fail an assertion inside of boost (when newing the processing thread) and the following is output on my screen

/root/src/boost.cmake/libs/thread/src/pthread/once.cpp:46: unsigned long &boost::detail::get_once_per_thread_epoch(): Assertion`!pthread_setspecific(epoch_tss_key,data)' failed.

I know that my join is working correctly because when the processing thread exits I output a message to my console. Does anyone know why this might happen?

An extra note... I have played around with my code a little bit and the methodology that I am using to clean up my boost threads appears to work in other parts of my program (for example, if I create the boost::thread in the parent class). However, it fails every time in the child class (which is an abstract class).

My start and stop methods looks like this...

 void ThreadMethod()
    {
      while(_runningThread)
      {
      }
    }
 void Start()
   {
     _runningThread = true;
     _thread = boost::make_shared<boost::thread>(&TestChildVirtualClass::ThreadMethod, this);
   };
   void Stop()
   {
     _runningThread = false;
     _thread->join();
     if( _thread )
     {
       _thread.reset();
     }
   };

However, I am having trouble recreating this issue in a test program (although it occurs every time in my actual program).

1
Debug the program and put a breakpoint on the code referenced in the question. It seems it should get called once per thread, so the second call should identify your problem.Steve Townsend
A successful join means the thread has finished execution. To execute again, you need to create another thread.Jerry Coffin
Which platform/compiler and boost version are you using?Vicente Botet Escriba

1 Answers

0
votes

The error could be a bug on Boost.Thread as there are some holes in the call_once implementation (#5752 boost::call_once() is unreliable on some platforms - see https://svn.boost.org/trac/boost/ticket/5752). This of course depends on which platform you are running your program.

Of course I maybe wrong.

You should also protect the access to _runningThread.