public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Ross Johnson <rpj@callisto.canberra.edu.au>
To: Pthreads-Win32 list <pthreads-win32@sources.redhat.com>
Subject: Re: Handle leak when using pthread mutex with win32 api threads
Date: Sat, 05 Mar 2005 21:41:00 -0000	[thread overview]
Message-ID: <1110058876.7988.19.camel@desk.home> (raw)
In-Reply-To: <acf63ee050305053177936d0b@mail.gmail.com>

On Sat, 2005-03-05 at 15:31 +0200, Dmitrii Sernii wrote:

> 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.
> 

Are you dynamic or static linking? If you're using the pthreads dll then
there would seem to be a problem in the library. If you're static
linking then read on ...

The library will be automatically creating POSIX thread handles (with
DETACHED status) because the pthread_mutex_lock() call needs a pthread
ID to track recursive mutex ownership. These pthread IDs are cleaned up
by the pthreads dll.c dllMain() switch. This cleanup doesn't happen
automatically when static linking, in which case it's necessary to
explicitly run the cleanup. There are four routines provided for this
(see README.NONPORTABLE for details):-

BOOL
pthread_win32_process_attach_np (void);

BOOL
pthread_win32_process_detach_np (void);

BOOL
pthread_win32_thread_attach_np (void);

BOOL
pthread_win32_thread_detach_np (void);


Regards.
Ross

> //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
> 

  reply	other threads:[~2005-03-05 21:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-05 13:31 Dmitrii Sernii
2005-03-05 21:41 ` Ross Johnson [this message]
2005-03-08  1:22 ` Ross Johnson
  -- strict thread matches above, loose matches on Subject: below --
2005-03-06 12:51 Bossom, John
2005-03-06  8:54 Dmitrii Sernii
2005-03-04 15:53 Dmitry Sernii
2005-03-04 17:06 ` Gottlob Frege
2005-03-04 22:27   ` Ross Johnson
2005-03-04 17:07 ` Gottlob Frege

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1110058876.7988.19.camel@desk.home \
    --to=rpj@callisto.canberra.edu.au \
    --cc=pthreads-win32@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).