]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
stdlib: Do not define once_flag, ONCE_FLAG_INIT for C++
authorFlorian Weimer <fweimer@redhat.com>
Tue, 11 Nov 2025 17:20:10 +0000 (18:20 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Tue, 11 Nov 2025 17:20:10 +0000 (18:20 +0100)
The definition of once_flag conflicts with std::once_flag in
if “using namespace std;” is active.

Updates commit a7ddbf456d97ac8d1aa7afd735e196a1488bd874
("Add once_flag, ONCE_FLAG_INIT and call_once to stdlib.h for C23").

Suggested-by: Jonathan Wakely <jwakely@redhat.com>
Reviewed-by: Collin Funk <collin.funk1@gmail.com>
manual/threads.texi
stdlib/stdlib.h

index 1e9be32d6691b5de093f40762450cd1786b3708c..b9d12479ca5a1c735a376e58ea16e25f21e2e9a3 100644 (file)
@@ -23,8 +23,15 @@ POSIX threads.
 This section describes the @glibcadj{} ISO C threads implementation.
 To have a deeper understanding of this API, it is strongly recommended
 to read ISO/IEC 9899:2011, section 7.26, in which ISO C threads were
-originally specified.  All types and function prototypes are declared
-in the header file @file{threads.h}.
+originally specified.
+
+All types and function prototypes are declared in the header file
+@file{threads.h}.  When compiling for C, some functionality is also
+available in @file{stdlib.h}.
+
+It is recommended that GNU systems use the functionality in
+@file{pthread.h} instead of @file{thread.h}, as they are more portable
+between C and C++.
 
 @menu
 * ISO C Threads Return Values:: Symbolic constants that represent a
@@ -203,11 +210,17 @@ called once in the presence of multiple, potentially calling threads.
 @deftp {Data Type} once_flag
 @standards{C11, threads.h}
 A complete object type capable of holding a flag used by @code{call_once}.
+
+As of C23, @code{once_flag} is also defined in @file{stdlib.h},
+but only for C, not for C++.
 @end deftp
 
 @defvr Macro ONCE_FLAG_INIT
 @standards{C11, threads.h}
 This value is used to initialize an object of type @code{once_flag}.
+
+As of C23, @code{ONCE_FLAG_INIT} is also defined in @file{stdlib.h},
+but only for C, not for C++.
 @end defvr
 
 @deftypefun void call_once (once_flag *@var{flag}, void (*@var{func}) (void))
index 8d4d194a465399baa2412e702f69deb1b15ccbd8..8416bd4446e9a7c3bc7de02d38b380e4918b11ad 100644 (file)
@@ -1159,11 +1159,13 @@ extern int ttyslot (void) __THROW;
 #endif
 
 #if __GLIBC_USE (ISOC23)
-# include <bits/types/once_flag.h>
+# ifndef __cplusplus
+#  include <bits/types/once_flag.h>
 
 /* Call function __FUNC exactly once, even if invoked from several threads.
    All calls must be made with the same __FLAGS object.  */
 extern void call_once (once_flag *__flag, void (*__func)(void));
+# endif /* !__cplusplus */
 
 /* Return the alignment of P.  */
 extern size_t memalignment (const void *__p);