]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
doc: Make an example multithread-safe.
authorBruno Haible <bruno@clisp.org>
Fri, 8 Jul 2022 17:26:40 +0000 (19:26 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 9 Oct 2022 07:30:42 +0000 (09:30 +0200)
Reported by Vivien Kraus <vivien@planete-kraus.eu> in
<https://lists.gnu.org/archive/html/bug-gettext/2022-07/msg00015.html>.

* gettext-tools/doc/gettext.texi (Libraries): Make the example code
multithread-safe.

gettext-tools/doc/gettext.texi

index b5a386e321d9bf2b3f46aecdceff147c8102b303..3b8240016af454bb2679a1c1d18891c732f64998 100644 (file)
@@ -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