From: Bruno Haible Date: Fri, 8 Jul 2022 17:26:40 +0000 (+0200) Subject: doc: Make an example multithread-safe. X-Git-Tag: v0.21.1~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c97f9967cb84e1228ce6a27faec51e2ba28a5c4;p=thirdparty%2Fgettext.git doc: Make an example multithread-safe. Reported by Vivien Kraus in . * gettext-tools/doc/gettext.texi (Libraries): Make the example code multithread-safe. --- 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