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%2Fthrd_yield.html | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 devdocs/c/thread%2Fthrd_yield.html (limited to 'devdocs/c/thread%2Fthrd_yield.html') diff --git a/devdocs/c/thread%2Fthrd_yield.html b/devdocs/c/thread%2Fthrd_yield.html new file mode 100644 index 00000000..7eb8bcad --- /dev/null +++ b/devdocs/c/thread%2Fthrd_yield.html @@ -0,0 +1,52 @@ +

thrd_yield

Defined in header <threads.h>
void thrd_yield(void);
+
(since C11)

Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run.

+

Parameters

(none)

+

Return value

(none)

+

Notes

The exact behavior of this function depends on the implementation, in particular on the mechanics of the OS scheduler in use and the state of the system. For example, a first-in-first-out realtime scheduler (SCHED_FIFO in Linux) would suspend the current thread and put it on the back of the queue of the same-priority threads that are ready to run (and if there are no other threads at the same priority, yield has no effect).

+

The POSIX equivalent of this function is sched_yield.

+

Example

#include <stdio.h>
+#include <time.h>
+#include <threads.h>
+ 
+// utility function: difference between timespecs in microseconds
+double usdiff(struct timespec s, struct timespec e)
+{
+    double sdiff = difftime(e.tv_sec, s.tv_sec);
+    long nsdiff = e.tv_nsec - s.tv_nsec;
+    if(nsdiff < 0) return 1000000*(sdiff-1) + (1000000000L+nsdiff)/1000.0;
+    else return 1000000*(sdiff) + nsdiff/1000.0;
+}
+ 
+// busy wait while yielding
+void sleep_100us()
+{
+    struct timespec start, end;
+    timespec_get(&start, TIME_UTC);
+    do {
+        thrd_yield();
+        timespec_get(&end, TIME_UTC);
+    } while(usdiff(start, end) < 100.0);
+}
+ 
+int main()
+{
+    struct timespec start, end;
+    timespec_get(&start, TIME_UTC);
+    sleep_100us();
+    timespec_get(&end, TIME_UTC);
+    printf("Waited for %.3f us\n", usdiff(start, end));
+}

Possible output:

+
Waited for 100.344 us

References

See also

+ +
+
(C11)
suspends execution of the calling thread for the given period of time
(function)
C++ documentation for yield
+

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

+
-- cgit v1.2.3