From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9366 invoked by alias); 6 Mar 2005 12:51:11 -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 9311 invoked from network); 6 Mar 2005 12:51:07 -0000 Received: from unknown (HELO nomad.cognos.com) (205.210.232.62) by sourceware.org with SMTP; 6 Mar 2005 12:51:07 -0000 Priority: normal Received: from sottemail1.ent.ad.cognos.com ([10.69.15.60]) by nomad.cognos.com with Microsoft SMTPSVC(6.0.3790.211); Sun, 6 Mar 2005 07:51:08 -0500 Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: Handle leak when using pthread mutex with win32 api threads Date: Sun, 06 Mar 2005 12:51:00 -0000 Message-ID: <89B41855D8749D49850F786F27B8AB3E063CBCF0@sottemail1.ent.ad.cognos.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: From: "Bossom, John" To: "Dmitrii Sernii" , X-OriginalArrivalTime: 06 Mar 2005 12:51:08.0068 (UTC) FILETIME=[2A7D6640:01C5224B] X-SW-Source: 2005/txt/msg00026.txt.bz2 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.=20 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=20 > > > mutex(PTHREAD_RECURSIVE_MUTEX_INITIALIZER); > > > > that line above is not thread safe. you might initialize the same=20 > > mutex multiple times. Not good. > > >=20 > The handle leak comes about because, in pthreads-win32,=20 > pthread_mutex_t is only a pointer, and the actual mutex struct is=20 > 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,=20 > you must always call pthread_mutex_destroy() to clean up, just the=20 > 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 =3D 500; DWORD id; HANDLE th[max_threads]; int i; for (i=3D0;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 =3D 500; DWORD id; pthread_t th[max_threads]; int i; for (i=3D0;i