標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
POSIX.4a |
両方 | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
int pthread_join(pthread_t thread, void **status);
#define _UNIX03_THREADS
#include <pthread.h>
int pthread_join(pthread_t thread, void **status);
呼び出しスレッドが、ターゲット thread の終了を待機できるように します。
pthread_t は、スレッドを一意的に識別する場合に使用される データ型です。これは pthread_create() によって戻され、スレッド ID を必要とする アプリケーションで使用されます。
status には、pthread_exit() の一部として終了スレッドが 渡す status 引数へのポインターが入ります。終了スレッドが戻りで終了した場合には、status には戻り値を指す ポインターが含まれています。スレッドが取り消された場合には、status は -1 に設定される ことがあります。
正常に実行された場合、pthread_join() は 0 を戻します。
Single UNIX Specification、バージョン 3 の特殊な動作: 正常に実行されなかった場合、pthread_join() はエラーを示すエラー番号を戻します。
⁄* CELEBP32 *⁄
#define _OPEN_THREADS
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
void *thread(void *arg) {
char *ret;
printf("thread() entered with argument '%s'¥n", arg);
if ((ret = (char*) malloc(20)) == NULL) {
perror("malloc() error");
exit(2);
}
strcpy(ret, "This is a test");
pthread_exit(ret);
}
main() {
pthread_t thid;
void *ret;
if (pthread_create(&thid, NULL, thread, "thread 1") != 0) {
perror("pthread_create() error");
exit(1);
}
if (pthread_join(thid, &ret) != 0) {
perror("pthread_create() error");
exit(3);
}
printf("thread exited with '%s'¥n", ret);
}
thread() entered with argument 'thread 1'
thread exited with 'This is a test'