{"id":4930,"date":"2022-12-20T18:36:58","date_gmt":"2022-12-20T21:36:58","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/pthread_once-man3p\/"},"modified":"2022-12-20T18:36:58","modified_gmt":"2022-12-20T21:36:58","slug":"pthread_once-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/pthread_once-man3p\/","title":{"rendered":"PTHREAD_ONCE (man3p)"},"content":{"rendered":"<h1 align=\"center\">PTHREAD_ONCE<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">pthread_once \u2014 dynamic package initialization<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <pthread.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int pthread_once(pthread_once_t *<i>once_control<\/i>, <br \/> void (*<i>init_routine<\/i>)(void)); <br \/> pthread_once_t <i>once_control<\/i> = PTHREAD_ONCE_INIT;<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The first call to <i>pthread_once<\/i>() by any thread in a process, with a given <i>once_control<\/i>, shall call the <i>init_routine<\/i> with no arguments. Subsequent calls of <i>pthread_once<\/i>() with the same <i>once_control<\/i> shall not call the <i>init_routine<\/i>. On return from <i>pthread_once<\/i>(), <i>init_routine<\/i> shall have completed. The <i>once_control<\/i> parameter shall determine whether the associated initialization routine has been called.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_once<\/i>() function is not a cancellation point. However, if <i>init_routine<\/i> is a cancellation point and is canceled, the effect on <i>once_control<\/i> shall be as if <i>pthread_once<\/i>() was never called.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the call to <i>init_routine<\/i> is terminated by a call to <i>longjmp<\/i>(), <i>_longjmp<\/i>(), or <i>siglongjmp<\/i>(), the behavior is undefined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The constant PTHREAD_ONCE_INIT is defined in the <i><pthread.h><\/i> header.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The behavior of <i>pthread_once<\/i>() is undefined if <i>once_control<\/i> has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, <i>pthread_once<\/i>() shall return zero; otherwise, an error number shall be returned to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_once<\/i>() function shall not return an error code of <b>[EINTR]<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>init_routine<\/i> recursively calls <i>pthread_once<\/i>() with the same <i>once_control<\/i>, the recursive call will not call the specified <i>init_routine<\/i>, and thus the specified <i>init_routine<\/i> will not complete, and thus the recursive call to <i>pthread_once<\/i>() will not return. Use of <i>longjmp<\/i>(), <i>_longjmp<\/i>(), or <i>siglongjmp<\/i>() within an <i>init_routine<\/i> to jump to a point outside of <i>init_routine<\/i> prevents <i>init_routine<\/i> from returning.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Some C libraries are designed for dynamic initialization. That is, the global initialization for the library is performed when the first procedure in the library is called. In a single-threaded program, this is normally implemented using a static variable whose value is checked on entry to a routine, as follows:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">static int random_is_initialized = 0; <br \/> extern void initialize_random(void);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int random_function() <br \/> { <br \/> if (random_is_initialized == 0) { <br \/> initialize_random(); <br \/> random_is_initialized = 1; <br \/> } <br \/> &#8230; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Operations performed after initialization. bodies\/ usr\/ <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">To keep the same structure in a multi-threaded program, a new primitive is needed. Otherwise, library initialization has to be accomplished by an explicit call to a library-exported initialization function prior to any use of the library.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For dynamic library initialization in a multi-threaded process, if an initialization flag is used the flag needs to be protected against modification by multiple threads simultaneously calling into the library. This can be done by using a mutex (initialized by assigning PTHREAD_MUTEX_INITIALIZER). However, the better solution is to use <i>pthread_once<\/i>() which is designed for exactly this purpose, as follows:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <pthread.h> static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT; <br \/> extern void initialize_random(void);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int random_function() <br \/> { <br \/> (void) pthread_once(&#038;random_is_initialized, initialize_random); <br \/> &#8230; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Operations performed after initialization. bodies\/ usr\/ <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If an implementation detects that the value specified by the <i>once_control<\/i> argument to <i>pthread_once<\/i>() does not refer to a <b>pthread_once_t<\/b> object initialized by PTHREAD_ONCE_INIT, it is recommended that the function should fail and report an <b>[EINVAL]<\/b> error.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><pthread.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  pthread_once \u2014 dynamic package initialization <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1594,1824],"class_list":["post-4930","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-man3p","tag-pthread_once"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4930"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4930\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}