From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12951 invoked by alias); 5 Mar 2005 13:31:30 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 12820 invoked from network); 5 Mar 2005 13:31:24 -0000 Received: from unknown (HELO rproxy.gmail.com) (64.233.170.204) by sourceware.org with SMTP; 5 Mar 2005 13:31:24 -0000 Received: by rproxy.gmail.com with SMTP id c51so755988rne for ; Sat, 05 Mar 2005 05:31:24 -0800 (PST) Received: by 10.38.65.21 with SMTP id n21mr3053rna; Sat, 05 Mar 2005 05:31:24 -0800 (PST) Received: by 10.38.75.68 with HTTP; Sat, 5 Mar 2005 05:31:24 -0800 (PST) Message-ID: Date: Sat, 05 Mar 2005 13:31:00 -0000 From: Dmitrii Sernii Reply-To: Dmitrii Sernii To: pthreads-win32@sources.redhat.com Subject: Re: Handle leak when using pthread mutex with win32 api threads Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-SW-Source: 2005/txt/msg00023.txt.bz2 > > > 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 #include #include 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 #include #include 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