From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/thread%2Fcall_once.html | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 devdocs/c/thread%2Fcall_once.html (limited to 'devdocs/c/thread%2Fcall_once.html') diff --git a/devdocs/c/thread%2Fcall_once.html b/devdocs/c/thread%2Fcall_once.html new file mode 100644 index 00000000..c49a1c35 --- /dev/null +++ b/devdocs/c/thread%2Fcall_once.html @@ -0,0 +1,55 @@ +

call_once, once_flag, ONCE_FLAG_INIT

Defined in header <threads.h>
void call_once( once_flag* flag, void (*func)(void) );
+
(1) (since C11)
typedef /* unspecified */ once_flag
+
(2) (since C11)
#define ONCE_FLAG_INIT /* unspecified */
+
(3) (since C11)
+1) Calls function func exactly once, even if invoked from several threads. The completion of the function func synchronizes with all previous or subsequent calls to call_once with the same flag variable.
+2) Complete object type capable of holding a flag used by call_once.
+3) Expands to a value that can be used to initialize an object of type once_flag.

Parameters

+ + +
flag - pointer to an object of type call_once that is used to ensure func is called only once
func - the function to execute only once

Return value

(none)

+

Notes

The POSIX equivalent of this function is pthread_once.

+

Example

#include <stdio.h>
+#include <threads.h>
+ 
+void do_once(void) {
+    puts("called once");
+}
+ 
+static once_flag flag = ONCE_FLAG_INIT;
+int func(void* data)
+{
+    call_once(&flag, do_once);
+}
+ 
+int main(void)
+{
+    thrd_t t1, t2, t3, t4;
+    thrd_create(&t1, func, NULL);
+    thrd_create(&t2, func, NULL);
+    thrd_create(&t3, func, NULL);
+    thrd_create(&t4, func, NULL);
+ 
+    thrd_join(t1, NULL);
+    thrd_join(t2, NULL);
+    thrd_join(t3, NULL);
+    thrd_join(t4, NULL);
+}

Output:

+
called once

References

See also

+
C++ documentation for call_once
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/thread/call_once +

+
-- cgit v1.2.3