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

pthread_create の構文

現在のプロセスへ新たに制御スレッドを追加するときは、pthread_create(3C) を使用します。

int pthread_create(pthread_t *restrict tid, const pthread_attr_t 
    *restrict tattr, void*(*start_routine)(void *), void *restrict arg);
#include <pthread.h>

pthread_attr_t() tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret; 

/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);

/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);

pthread_create() 関数は、必要な状態動作を持つ attr で呼び出されます。start_routine は新しいスレッドで実行する関数です。start_routine で指定した関数が終了すると、スレッドはその関数の戻り値を終了状態に設定して終了します。pthread_create の構文」を参照してください。

pthread_create() が正常終了すると、生成されたスレッドの識別子が tid の指す記憶場所に格納されます。

pthread_create() の属性引数 (デフォルト属性) として NULL を指定すると、デフォルトスレッドが生成されます。()tattr は初期化されると、デフォルト動作を獲得します。