views:

80

answers:

1

I think I am making a simple mistake, but since I noticed there are many boost experts here, I thought I would ask for help.

I am trying to use boost threads(1_40) on windows xp. The main program loads a dll, starts the thread like so (note this is not in a class, the static does not mean static to a class but private to the file).

static boost::thread network_thread;
static bool quit = false;
HANDLE quitEvent;

//some code omitted for clarity, ask if you think it would help
void network_start()
{
  HANDLE *waitHandles = (HANDLE*)malloc(3 * sizeof(HANDLE));
  waitHandles[0] = quitEvent;
  waitHandles[1] = recvEvent;
  waitHandles[2] = pendingEvent;
  do {
      //read network stuff, or quit event
      dwEvents =WaitForMultipleObjects(3, waitHandles, FALSE, timeout);
   } while (!quit)
}

DllClass::InitInstance() 
{
}

DllClass::ExportedFunction()
{
   network_thread = boost::thread(boost::bind<void>(network_start));
}


DllClass::ExitInstance()
{
  //signal quit (which works)
   quit = true;
   SetEvent(QuitEvent);
   //the following code is slightly verbose because I'm trying to figure out what's wrong
    try {
       if (network_thread.joinable() ) {
           network_thread.join();
         } else {
            TRACE("Too late!");
        }
   } catch (boost::thread_interrupted&) {
        TRACE("NET INTERRUPTED");
    }
}

The problem is that the main thread is hanging on the join, and the network thread is hanging at the end of _endthreadex. What am I misunderstanding?

+2  A: 

You are not supposed to create/end threads in InitInstance/ExitInstance,

see http://support.microsoft.com/default.aspx?scid=kb;EN-US;142243 for more info. Also, see http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx about DllMain in general.

Chris O
Well, I should be more clear in that the thread is not CREATED in the InitInstance, but I do try to destroy it from the ExitInstance. This is likely the source of the problem, I'll investigate, thanks.
FranticPedantic
That was the problem. The sick part is that the coworker who actually knows MFC even put a "destroy" method in the interface..silly me.
FranticPedantic