From 5c97f9967cb84e1228ce6a27faec51e2ba28a5c4 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 8 Jul 2022 19:26:40 +0200 Subject: [PATCH] doc: Make an example multithread-safe. Reported by Vivien Kraus in . * gettext-tools/doc/gettext.texi (Libraries): Make the example code multithread-safe. --- gettext-tools/doc/gettext.texi | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gettext-tools/doc/gettext.texi b/gettext-tools/doc/gettext.texi index b5a386e32..3b8240016 100644 --- a/gettext-tools/doc/gettext.texi +++ b/gettext-tools/doc/gettext.texi @@ -2947,11 +2947,14 @@ invocation. However, you usually don't need to export and document this initialization function: It is sufficient that all entry points of the library call the initialization function if it hasn't been called before. The typical idiom used to achieve this is a static boolean variable that -indicates whether the initialization function has been called. Like this: +indicates whether the initialization function has been called. If the +library is meant to be used in multithreaded applications, this variable +needs to be marked @code{volatile}, so that its value get propagated +between threads. Like this: @example @group -static bool libfoo_initialized; +static volatile bool libfoo_initialized; static void libfoo_initialize (void) @@ -2982,6 +2985,10 @@ foo_refcount (struct foo *argument) @end group @end example +@noindent +The more general solution for initialization functions, POSIX +@code{pthread_once}, is not needed in this case. + @item The usual declaration of the @samp{_} macro in each source file was -- 2.47.2