DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 62HE98hM941861 Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 62HE98hM941861 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=NAzQdWNX X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20C614C3187E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1773756547; bh=HHrv+QZscHh4s2Q+EtAI3BtvnisCSF6pSZH9vgxOSWU=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=NAzQdWNXP2KrrK1+/CQs9OyLDkgWT+H8n3bqxYVeYfJj5iHJdIbDB221cHqWweO+e PkszqtsXJkpNnWgO3Ql6xuj5BmoKEVA4GjR1H/Q871pJCbx3olzcrX2O+OGcyEwwiq M8BaY7B40KuZTwmP5JFC7V5j8VpOA6bt/vMnY0vc= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 360B14B9700A Date: Tue, 17 Mar 2026 15:08:19 +0100 To: Noel Grandin Subject: Re: std::mutex bug: Windows handle growth Message-ID: Mail-Followup-To: Noel Grandin , Frank Eskesen , Cygin Mailing List References: <2d48a740-ce2b-4259-8947-f5c875dd1672 AT hvc DOT rr DOT com> <406b3882-15be-4d24-945c-42f1e2715e87 AT gmail DOT com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <406b3882-15be-4d24-945c-42f1e2715e87@gmail.com> X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Corinna Vinschen via Cygwin Reply-To: cygwin AT cygwin DOT com Cc: Corinna Vinschen , Frank Eskesen , Cygin Mailing List Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" On Mar 17 09:06, Noel Grandin via Cygwin wrote: > Hi > > Hazarding a guess here: > > On 3/16/2026 11:39 PM, Frank Eskesen via Cygwin wrote: > > I've run into a problem that occurs on Cygwin that doesn't occur on Linux > > systems: When a std::mutex is used, it doesn't clean up a Windows handle that > > it uses. This sample program demonstrates the problem, failing in under 60 > This code looks suspicious: > > pthread_mutex::destroy () > { > if (condwaits || trylock ()) > // Do not destroy a condwaited or locked mutex > return EBUSY; > > I think it should be > > if (condwaits || !trylock ()) No, checking trylock() is correct. pthread_mutex_destroy() must not destroy a busy mutex. trylock() returns != 0 if the lock is busy. Given the implementation of std::mutex is based on pthread_mutex, Frank's code is wrong, or at least somewhat borderline. The mutex should first be unlocked before leaving do_something(). The reason this problem doesn't occur on Linux is the fact that mutexes are implemented without allocating a system object, so the pthread_mutex_destroy() function is basically a no-op, after checking if the lock is busy and returning 0 or EBUSY, whatever is right. Corinna -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple