マルチスレッドのプログラミング

thr_join(3THR)

thr_join(3THR) 関数はスレッドの終了を待ちます。(POSIX スレッドについては、「pthread_join(3THR)」を参照)。


#include <thread.h>

int thr_join(thread_t tid, thread_t *departedid, void **status);

指定したスレッドの終了待ち


#include <thread.h>

thread_t tid;
thread_t departedid;
int ret;
int status;

/* スレッド「tid」の終了待ち、status の指定あり */
ret = thr_join(tid, &departedid, (void**)&status);

/* スレッド「tid」の終了待ち、status の指定なし */
ret = thr_join(tid, &departedid, NULL);

/* スレッド「tid」の終了待ち、departedid と status の指定なし */
ret = thr_join(tid, NULL, NULL); 

tid(thread_t) 0 の場合は、thr_join() はプロセス内の切り離されていない任意のスレッドの終了を待ちます。つまり、スレッド識別子を指定しなければ、切り離されていないスレッドのどれかが終了すると thr_join() が復帰します。

任意のスレッドの終了待ち


#include <thread.h>

thread_t tid;
thread_t departedid;
int ret;
int status;

/* スレッド「tid」の終了待ち、status の指定あり */
ret = thr_join(NULL, &departedid, (void **)&status); 

thr_join() でスレッド識別子として NULL を指定すると、プロセス内の切り離されていない任意のスレッドの終了を待ちます。departedid には、終了したスレッドのスレッド識別子が格納されます。