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]
Other format: [Raw text]

RE: Handle leak when using pthread mutex with win32 api threads


My hands have been out of the pthreads-win32 code base for some time
(1998!)

However, I do know that on UNIX you can and will leak resources if
you do not treat your main line like any other thread. In otherwords,
call pthread_exit() to end your mainline. 

I believe this is documented in the book written by Bil Lewis.

Cheers,

John Bossom



-----Original Message-----
From: pthreads-win32-owner@sources.redhat.com
[mailto:pthreads-win32-owner@sources.redhat.com] On Behalf Of Dmitrii
Sernii
Sent: Saturday, March 05, 2005 8:31 AM
To: pthreads-win32@sources.redhat.com
Subject: Re: Handle leak when using pthread mutex with win32 api threads

> > >        static pthread_mutex_t 
> > > mutex(PTHREAD_RECURSIVE_MUTEX_INITIALIZER);
> >
> > that line above is not thread safe.  you might initialize the same 
> > mutex multiple times.  Not good.
> >
> 
> The handle leak comes about because, in pthreads-win32, 
> pthread_mutex_t is only a pointer, and the actual mutex struct is 
> malloced by the library, in this case, the first time you call
pthread_mutex_lock().
> That is, the library just calls pthread_mutex_init() for you. Also, 
> you must always call pthread_mutex_destroy() to clean up, just the 
> same as if you had called pthread_mutex_init() yourself.

I've made small corrections in test sample, so now mutex initialised
only once, and i've added pthread_mutex_destroy() function call. But the
problem still remains.
Here is two test applications - one of them uses API threads, and the
other pthreads. Program with pthreads don't have any leaks, while
program with API threads produces lots of them.

//program with handle leaks
#include <pthread.h>
#include <windows.h>
#include <conio.h>

pthread_mutex_t mutex(PTHREAD_RECURSIVE_MUTEX_INITIALIZER);

DWORD WINAPI threadProc(void *param)
{
	pthread_mutex_lock(&mutex);
	pthread_mutex_unlock(&mutex);
	return 0;
}

void main()
{
	const int max_threads = 500;
	DWORD id;
	HANDLE th[max_threads];
	int i;
	for (i=0;i<max_threads;i++)
	th[i]=CreateThread(0,0,threadProc,0,0,&id);

	for (i=0;i<max_threads;i++)
	{
		WaitForSingleObject(th[i],INFINITE);
		CloseHandle(th[i]);
	}
	pthread_mutex_destroy(&mutex);
	getch();
}

==========================

//program without handle leaks
#include <pthread.h>
#include <windows.h>
#include <conio.h>


pthread_mutex_t mutex(PTHREAD_RECURSIVE_MUTEX_INITIALIZER);

void* threadProc(void *param)
{
	pthread_mutex_lock(&mutex);
	pthread_mutex_unlock(&mutex);
    return 0;
}

void main()
{
	const int max_threads = 500;
	DWORD id;
	pthread_t th[max_threads];
	int i;

	for (i=0;i<max_threads;i++)
			pthread_create(&(th[i]),0,threadProc,(void*)i);

	for (i=0;i<max_threads;i++)
		pthread_join(th[i],(void**)&id);

	pthread_mutex_destroy(&mutex);
	getch();
}

Best regards,
Dmitrii Sernii 
  
       This message may contain privileged and/or confidential information.  If you have received this e-mail in error or are not the intended recipient, you may not use, copy, disseminate or distribute it; do not open any attachments, delete it immediately from your system and notify the sender promptly by e-mail that you have done so.  Thank you. 
 


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