📄 pthread_once.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>pthread_once</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="pthread_once"></a> <a name="tag_03_554"></a><!-- pthread_once --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_554_01"></a>NAME</h4><blockquote>pthread_once - dynamic package initialization</blockquote><h4><a name="tag_03_554_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('THR')">THR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/pthread.h.html">pthread.h</a>><br><br> int pthread_once(pthread_once_t *</tt><i>once_control</i><tt>,<br> void (*</tt><i>init_routine</i><tt>)(void));<br> pthread_once_t</tt> <i>once_control</i> <tt>= PTHREAD_ONCE_INIT; <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_554_03"></a>DESCRIPTION</h4><blockquote><p>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 callthe <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><p>The <i>pthread_once</i>() function is not a cancellation point. However, if <i>init_routine</i> is a cancellation point and iscanceled, the effect on <i>once_control</i> shall be as if <i>pthread_once</i>() was never called.</p><p>The constant PTHREAD_ONCE_INIT is defined in the <a href="../basedefs/pthread.h.html"><i><pthread.h></i></a> header.</p><p>The behavior of <i>pthread_once</i>() is undefined if <i>once_control</i> has automatic storage duration or is not initializedby PTHREAD_ONCE_INIT.</p></blockquote><h4><a name="tag_03_554_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>pthread_once</i>() shall return zero; otherwise, an error number shall be returned to indicatethe error.</p></blockquote><h4><a name="tag_03_554_05"></a>ERRORS</h4><blockquote><p>The <i>pthread_once</i>() function may fail if:</p><dl compact><dt>[EINVAL]</dt><dd>If either <i>once_control</i> or <i>init_routine</i> is invalid.</dd></dl><p>The <i>pthread_once</i>() function shall not return an error code of [EINTR].</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_554_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_554_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_554_08"></a>RATIONALE</h4><blockquote><p>Some C libraries are designed for dynamic initialization. That is, the global initialization for the library is performed whenthe first procedure in the library is called. In a single-threaded program, this is normally implemented using a static variablewhose value is checked on entry to a routine, as follows:</p><pre><tt>static int random_is_initialized = 0;extern int initialize_random();<br>int random_function(){ if (random_is_initialized == 0) { initialize_random(); random_is_initialized = 1; } ... /* Operations performed after initialization. */}</tt></pre><p>To keep the same structure in a multi-threaded program, a new primitive is needed. Otherwise, library initialization has to beaccomplished by an explicit call to a library-exported initialization function prior to any use of the library.</p><p>For dynamic library initialization in a multi-threaded process, a simple initialization flag is not sufficient; the flag needsto be protected against modification by multiple threads simultaneously calling into the library. Protecting the flag requires theuse of a mutex; however, mutexes have to be initialized before they are used. Ensuring that the mutex is only initialized oncerequires a recursive solution to this problem.</p><p>The use of <i>pthread_once</i>() not only supplies an implementation-guaranteed means of dynamic initialization, it provides anaid to the reliable construction of multi-threaded and realtime systems. The preceding example then becomes:</p><pre><tt>#include <pthread.h>static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;extern int initialize_random();<br>int random_function(){ (void) pthread_once(&random_is_initialized, initialize_random); ... /* Operations performed after initialization. */}</tt></pre><p>Note that a <b>pthread_once_t</b> cannot be an array because some compilers do not accept the construct<b>&<array_name></b>.</p></blockquote><h4><a name="tag_03_554_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_554_10"></a>SEE ALSO</h4><blockquote><p>The Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/pthread.h.html"><i><pthread.h></i></a></p></blockquote><h4><a name="tag_03_554_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 5. Included for alignment with the POSIX Threads Extension.</p></blockquote><h4><a name="tag_03_554_12"></a>Issue 6</h4><blockquote><p>The <i>pthread_once</i>() function is marked as part of the Threads option.</p><p>The [EINVAL] error is added as a may fail case for if either argument is invalid.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -