[PATCH v2] pthread: add PTHREAD_NULL

Alyssa Ross hi@alyssa.is
Thu Nov 13 12:44:13 GMT 2025


New in POSIX.1-2024.

In htl, pthread_t values are integers starting at 1, so 0 is a safe
null value.

In nptl, pthread_t values are non-null pointers, so 0 is a safe null
value.
---
v2: hide behind __USE_XOPEN2K24
v1: https://inbox.sourceware.org/libc-alpha/20251101193213.684326-2-hi@alyssa.is/

 manual/threads.texi    | 5 +++++
 sysdeps/htl/pthread.h  | 4 ++++
 sysdeps/nptl/pthread.h | 5 +++++
 3 files changed, 14 insertions(+)

diff --git a/manual/threads.texi b/manual/threads.texi
index b9d12479ca..6ccbb0a3ab 100644
--- a/manual/threads.texi
+++ b/manual/threads.texi
@@ -1112,6 +1112,11 @@ This function gets the kind of mutex @var{mutex} is.
 @node POSIX Threads Other APIs
 @subsection POSIX Threads Other APIs
 
+@deftypevr Macro pthread_t PTHREAD_NULL
+@standards{POSIX.1-2024, pthread.h}
+A value for pthread_t that is not considered equal to any thread ID.
+@end deftypevr
+
 @deftypefun int pthread_equal (pthread_t @var{thread1}, pthread_t @var{thread2})
 Compares two thread IDs.  If they are the same, returns nonzero, else returns zero.
 @manpagefunctionstub{pthread_equal, 3}
diff --git a/sysdeps/htl/pthread.h b/sysdeps/htl/pthread.h
index a299fec278..af322e5b8a 100644
--- a/sysdeps/htl/pthread.h
+++ b/sysdeps/htl/pthread.h
@@ -55,6 +55,10 @@ __BEGIN_DECLS
 
 #include <bits/pthread.h>
 
+#ifdef __USE_XOPEN2K24
+# define PTHREAD_NULL ((pthread_t)0)
+#endif
+
 /* Possible values for the process shared attribute.  */
 #define PTHREAD_PROCESS_PRIVATE __PTHREAD_PROCESS_PRIVATE
 #define PTHREAD_PROCESS_SHARED __PTHREAD_PROCESS_SHARED
diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h
index 92957a620d..f3b66d7bf4 100644
--- a/sysdeps/nptl/pthread.h
+++ b/sysdeps/nptl/pthread.h
@@ -33,6 +33,11 @@
 # include <bits/pthread_stack_min-dynamic.h>
 #endif
 
+#ifdef __USE_XOPEN2K24
+# define PTHREAD_NULL ((pthread_t)0)
+#endif
+
+
 /* Detach state.  */
 enum
 {

base-commit: 2254e871f49b8844a77367318d8b9cd8ca839374
-- 
2.51.0



More information about the Libc-alpha mailing list