std::mutex bug: Windows handle growth
kikairoya
kikairoya@gmail.com
Tue Sep 22 05:21:10 GMT 2026
Hi Mark,
I apologize for the insufficient context.
The top of the thread is: https://cygwin.com/pipermail/cygwin/2026-March/259538.html
and I meant to reply to: https://cygwin.com/pipermail/cygwin/2026-March/259546.html
The bug in libstdc++'s std::mutex has already been fixed in the Cygwin specific patch:
https://www.cygwin.com/cgit/cygwin-packages/gcc/tree/0013-Cygwin-libstdc-fix-a-handle-leak-in-mutex_base.patch?id=fd6b75538ab8f72c64b59835a034d55e388040d3
but std::shared_mutex needs a similar fix.
Thanks,
2026年9月22日 13:56:34 (+09:00) で、Mark Geisert via Cygwin さんが書きました:
> Hello kikairoya,
>
> > Hi,
> > std::shared_mutex (actually, __shared_mutex_pthread) appears to need a similar fix.
> > Could you take a look?
> > 2026年3月18日 4:09:53 (+09:00) で、Corinna Vinschen via Cygwin さんが書きました:
> >> The actual problem here is the `#ifdef __GTHREAD_MUTEX_INIT'.
> >> __GTHREAD_MUTEX_INIT translates into PTHREAD_MUTEX_INIT. If this is
> >> defined, you will notice the there's *no* destructor defined for class
> >> __mutex_base. Apparently the assumption is made that the underlying
> >> pthread_mutex_t type is a trivial type which doesn't need a destroy(),
> >> if PTHREAD_MUTEX_INIT exists.
> >> However, this is wrong for Cygwin. It defines PTHREAD_MUTEX_INIT, but
> >> it still needs a pthread_mutex_destroy() call, because the pthread_mutex_t
> >> type is not a trivial one.
> >> Thus, we need a destructor even if __GTHREAD_MUTEX_INIT is defined.
> >> A potential fix would be diff --git a/libstdc++-v3/include/bits/std_mutex.h b/libstdc++-v3/include/bits/std_mutex.h
> >> index 7ef33fe5d0d7..f060c75abf21 100644
> >> --- a/libstdc++-v3/include/bits/std_mutex.h
> >> +++ b/libstdc++-v3/include/bits/std_mutex.h
> >> @@ -68,6 +68,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >> __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
> >> constexpr __mutex_base() noexcept = default;
> >> +#ifdef __CYGWIN__
> >> + ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
> >> +#endif
> >> #else
> >> __native_type _M_mutex;
> >>
> We're going to need a bit more context to evaluate this. There is no file named std_mutex.h in the newlib or Cygwin source trees. Is this perhaps a file in the gcc-g++ source tree? Or somewhere else?
>
> Can you point us to the original email that started the chain that Corinna replied to?
>
> ..mark
>
--
Tomohiro Kashiwada (@kikairoya)
More information about the Cygwin
mailing list