PTHREAD_MUTEX_INITIALIZER and constinit
Jeremy Drake
cygwin@jdrake.com
Sat Jun 14 23:41:38 GMT 2025
Consider the following -std=c++20:
#include <pthread.h>
constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER;
With g++ 12.4.0, this succeeds, but with clang 20.1.6 it fails:
pthreadtest.cpp:3:27: error: variable does not have a constant initializer
3 | constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
pthreadtest.cpp:3:1: note: required by 'constinit' specifier here
3 | constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER;
| ^~~~~~~~~
pthreadtest.cpp:3:33: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
3 | constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER;
| ^
/usr/include/pthread.h:49:35: note: expanded from macro 'PTHREAD_MUTEX_INITIALIZER'
49 | #define PTHREAD_MUTEX_INITIALIZER PTHREAD_NORMAL_MUTEX_INITIALIZER_NP
| ^
/usr/include/pthread.h:47:45: note: expanded from macro 'PTHREAD_NORMAL_MUTEX_INITIALIZER_NP'
47 | #define PTHREAD_NORMAL_MUTEX_INITIALIZER_NP (pthread_mutex_t)19
| ^
1 error generated.
It seems the standard disallows this sort of cast pretty comprehensively.
Is there some way that Cygwin's pthread initializer macros can be made
compatible with constinit?
More information about the Cygwin
mailing list