]> 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>
Fri, 8 Jul 2022 17:26:40 +0000 (19:26 +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 152eb030c564af385033f3d61d38e9b84fe68e7a..c08243a7b6f65f2b3486215ac2490d070d5439ff 100644 (file)
@@ -2952,11 +2952,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)
@@ -2987,6 +2990,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