標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
POSIX.4a | 両方 | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
void pthread_yield(NULL);
pthread_yield() 関数を使用すると、スレッドは、ほかのスレッドが実行する機会をもてるようにプロセッサーの制御を放棄できます。
関数に対するパラメーターは、NULL 以外の値が予約されているので NULL で なければなりません。
pthread_yield() 関数がプロセッサーを解放する速度は、_EDC_PTHREAD_YIELD および _EDC_PTHREAD_YIELD_MAX 環境変数を使用して構成できます。_EDC_PTHREAD_YIELD 環境変数は、プロセッサーを即時に解放するか、遅延後にプロセッサーを解放するように pthread_yield() 関数を構成するのに使用されます。_EDC_PTHREAD_YIELD_MAX 環境変数は、最大遅延をデフォルト (32 ミリ秒) より小さい値に変更するのに使用されます。
環境変数 _EDC_PTHREAD_YIELD および _EDC_PTHREAD_YIELD_MAX について詳しくは、「z/OS XL C/C++ プログラミング・ガイド」の『環境変数の使用』を参照してください。
pthread_yield() は、値を戻しません。
設定される errno 値はありません。
⁄* 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