>From e191b4e30d96324f374655425e296a735d4c4051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Arruga=20Vivas?= Date: Fri, 9 Jun 2023 15:26:13 +0200 Subject: [PATCH] Update manual advice on multiple threads * gettext-tools/doc/gettext.texi (Libraries): Suggest call_once usage. Reference to phread_once for older systems. --- gettext-tools/doc/gettext.texi | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/gettext-tools/doc/gettext.texi b/gettext-tools/doc/gettext.texi index 75f87291c..71b83f417 100644 --- a/gettext-tools/doc/gettext.texi +++ b/gettext-tools/doc/gettext.texi @@ -3020,21 +3020,19 @@ you need to create one, containing at least the @code{bindtextdomain} 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. 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: +The modern idiom used to achieve this is a static variable of type +@code{once_flag} and the usage @code{call_once} to perform only once +the initialization (@pxref{Call Once,,,libc,The GNU C Library +manual}). Like this: @example @group -static volatile bool libfoo_initialized; +static once_flag libfoo_initialized = ONCE_FLAG_INIT; static void libfoo_initialize (void) @{ bindtextdomain (PACKAGE, LOCALEDIR); - libfoo_initialized = true; @} /* This function is part of the exported API. */ @@ -3042,8 +3040,7 @@ struct foo * create_foo (...) @{ /* Must ensure the initialization is performed. */ - if (!libfoo_initialized) - libfoo_initialize (); + call_once (&libfoo_initialized, libfoo_initialize); ... @} @@ -3060,8 +3057,9 @@ foo_refcount (struct foo *argument) @end example @noindent -The more general solution for initialization functions, POSIX -@code{pthread_once}, is not needed in this case. +Older systems have the equivalent POSIX type @code{pthread_once_t} and +the function @code{pthread_once}, or similar primitives provided by +the thread library. @item The usual declaration of the @samp{_} macro in each source file was -- 2.40.1