Boost logo

Boost Users :

Subject: [Boost-users] [Thread] Using thread accross libraries
From: Sébastien Gallou (sebastien.gallou_at_[hidden])
Date: 2013-11-26 04:16:34


Hi all,

first, thanks for this great library.

I'm using Boost::thread and experiencing a problem when trying to interrupt
thread executed in a dynamic library (DLL, under Windows).
I reproduced it in a minimal example, that I join to this post (I don't
know if attachment is authorized so I also put it on my
GDrive<https://drive.google.com/file/d/0B3dXqBwTbfcAQ1E0MDRfU0hxNVE/edit?usp=sharing>,
and copy the code at the end of this mail).
In this code, I start a thread in the main, calling a function "doWork" of
a plugin, dynamically loaded.
The doWork just do some sleep and wait for boost::thread_interrupted
exception.
The problem is that the exception is never received when the main call
interrupt on the thread.

If the doWork code is in the main application, all works well.

Trying to debug into Boost, I'm seeing that in the interruptible_wait
function, the test "if(detail::get_current_thread_data() &&
detail::get_current_thread_data()->interruption_enabled)" doesn't give the
same result if code is in DLL (test result is false) or in the main (true).

What is the reason of this behaviour ?
Thanks for your help,

Sébastien Gallou

Here is the code :

*TestBoostThread.cpp :*

IPlugin* Plugin;
void doWork(){
   try
   {
      Plugin->doWork();
   }
   catch (boost::thread_interrupted&)
   {
      // End-of-thread exception was not catch by plugin,
      // it's a developer's error, we have to report it
      std::cout << "Plugin didn't catch boost::thread_interrupted.";
   }}
int main(int argc, char* argv[]){
   HMODULE libraryHandle = LoadLibrary(L"Dll.dll");
   boost::function<IPlugin* ()> construct = (IPlugin*
(*)(void))GetProcAddress(libraryHandle, "construct");
   Plugin=construct();

   boost::shared_ptr<boost::thread> thread =
boost::shared_ptr<boost::thread>(new boost::thread(doWork));

   Sleep(1500);

   thread->interrupt();
   thread->join();

   FreeLibrary(libraryHandle);

        return 0;}

*Dll.cpp :*

class MyPlugin : public IPlugin{
   virtual ~MyPlugin(){}

   virtual void doWork()
   {
      try
      {
         while(1)
         {
            std::cout << "MyPlugin is running...";

            // Give a chance to exit plugin thread
            boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
         };
      }
      catch (boost::thread_interrupted&)
      {
         std::cout << "MyPlugin is stopped...";
      }
   }};
extern "C" __declspec(dllexport) IPlugin* construct(){
   return new MyPlugin;}





Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net