Boost logo

Boost :

From: Petr Ovchenkov (ptr_at_[hidden])
Date: 2001-10-15 01:28:33


>>>>> "JHL" == Jonathan H Lundquist <Jonathan> writes:

 JHL> I have a worker thread running in a dll, for which I have no
 JHL> control over the process lifetime. When the process exits the
 JHL> dll is going to run its static destructors. The thread accesses
 JHL> static data (properly synchronized), so I need to arrange for it
 JHL> to terminate before static destructors are run. If I give it
 JHL> some kind of signal to terminate, and then attempt to wait for
 JHL> it to do so in dllmain DLL_PROCESS_DETACH, then I deadlock due
 JHL> to the serialization of dllmain.

 JHL> It looks to me like boost threads would have exactly the same
 JHL> problem if the main thread attempted to join worker threads at
 JHL> DLL_PROCESS_DETACH time. Any ideas on how one can correctly
 JHL> ensure all but the main thread have terminated before static
 JHL> destructors are run in a dll would be appreciated. Thank you.

This is Windows, isn't it? I think that only workaround is to avoid
start threads from ANY static object. The behavior of such processes
in termination time is very differ for different Windows. NT is
still can terminate such process (not normally, but from task
manager), while Win 95 and Win 98 can't do it at all (one way ---
reboot). You correctly say, that reason is termination of main
thread while other threads exists. This situation can't be correctly
processed by Win's scheduler (NT, 95, 98, I don't know about 2K or XP).

Workaround is simple: don't start threads from static. If you have
static, you can make workaround like:

class YouObjectWithThread {
  public:
    YouObjectWithThread()
     { start_thread_here(); }

    ~YouObjectWithThread()
     { stop_thread_here(); }
};

YouObjectWithThread *O1;

YouObjectWithThread& O2 = *O1; // don't refer before 'O1 = new '
...

int main( int, char ** )
{
  O1 = new YouObjectWithThread();
  ...
  ...
  wait();

  delete O1;
  return 0;
}

Fuctionality will be the same as with static YouObjectWithThread.^{if
you work from DLL, change main to DllMail}

Hope this help,

   - ptr


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