pthread_yield() — Release the processor to other threads
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a | both | POSIX(ON)
|
Format
#define _OPEN_THREADS
#include <pthread.h>
void pthread_yield(NULL);
General description
The pthread_yield() function allows a thread to give up control of a processor so that another thread can have the opportunity to run.
The parameter to the function must be NULL, because non-NULL values are reserved.
The speed at which the pthread_yield() function releases a processor can be configured by using the _EDC_PTHREAD_YIELD and _EDC_PTHREAD_YIELD_MAX environment variables. The _EDC_PTHREAD_YIELD environment variable is used to configure the pthread_yield() function to release the processor immediately, or to release the processor after a delay. The _EDC_PTHREAD_YIELD_MAX environment variable is used to change the maximum delay to a value less than the default (32 milliseconds).
For more information about the _EDC_PTHREAD_YIELD and _EDC_PTHREAD_YIELD_MAX environment variables, see “Using Environment Variables” in z/OS XL C/C++ Programming Guide.
Returned value
pthread_yield() returns no values.
There are no documented errno values.
Example
/* CELEBP53 */
#define _OPEN_THREADS
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread(void *arg) {
/* A simple loop with only puts() would allow a thread to write several
lines in a row.
With pthread_yield(), each thread gives another thread a chance before
it writes its next line */
while (1) {
puts((char*) arg);
pthread_yield(NULL);
}
}
main() {
pthread_t t1, t2, t3;
if (pthread_create(&t1, NULL, thread, "thread 1") != 0) {
perror("pthread_create() error");
exit(1);
}
if (pthread_create(&t2, NULL, thread, "thread 2") != 0) {
perror("pthread_create() error");
exit(2);
}
if (pthread_create(&t3, NULL, thread, "thread 3") != 0) {
perror("pthread_create() error");
exit(3);
}
sleep(1);
exit(0); /* this will tear all threads down */
}
thread 1
thread 3
thread 2
thread 1
thread 3
thread 2
thread 1
thread 3
thread 2
thread 1
thread 3