This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problem using pthreads as static lib (handles lost)


Hello!

I'm using the pthreads-win32 implementation as a static library linked to a
test program. When my program is running it creates a thread for each socket
connections. When I stress test it, it uses windows handles and memory
resources till the system (Windows 2000 or NT) is out of handles. When I run
it with the DLL library it works.

My source looks like this:

void * func(void * arg)
{
  pthread_t t;

  <<wait for connection>
  pthread_create(&t, NULL, func, NULL);
  pthread_detach(t);
  <<do something>>
  return 0; 
}
 
int
main()
{
  pthread_t t;

  pthread_create(&t, NULL, func, NULL);
  pthread_detach(t);
  <<do something else>>
}

The handles are lost here:

  /*
   * Run the caller's routine; no cancelation or other exceptions will
   * be honoured.
   */
  status = self->exitStatus = (*start) (arg);

#endif /* __cplusplus */

#endif /* _MSC_VER */

  (void) pthread_mutex_destroy(&self->cancelLock);
  ptw32_callUserDestroyRoutines(self);

#if ! defined (__MINGW32__) || defined (__MSVCRT__)
  _endthreadex ((unsigned) status);
#else
  _endthread ();
#endif

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The _endthreadex function does not close the windows handle for the thread,
nor for the cancelEvent. In the DLL library this is done by DllMain(). If
you want to use the library static you have to add something like this
before the _endthreadex:

  if (self->detachState == PTHREAD_CREATE_DETACHED)
  {
     if (self->cancelEvent != NULL)
     {
        CloseHandle (self->cancelEvent);
     }
     CloseHandle(self->threadH);
     free (self);
  }
--------------------------------
 #if ! defined (__MINGW32__) || defined (__MSVCRT__)
   _endthreadex ((unsigned) status);
 #else

I think this is much cleaner and faster than the dll solution.

Regards
Marius

--- 
Dipl.-Inform. Marius Heuler 
CD InfoSim Networking Solutions AG i.G.
Sedanstr. 27 - 97082 Wuerzburg 
Fon: +49 931 4194 590  Fax: +49 931 4194 589 
Mobile: +49 171 43 66 938
mailto:marius@heuler.net, mailto:marius.heuler@infosim.net 
http://www.heuler.net

--- 
Dipl.-Inform. Marius Heuler 
CD InfoSim Networking Solutions AG i.G.
Sedanstr. 27 - 97082 Wuerzburg 
Fon: +49 931 4194 590  Fax: +49 931 4194 589 
Mobile: +49 171 43 66 938
mailto:marius@heuler.net, mailto:marius.heuler@infosim.net 
http://www.heuler.net

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]