linux--スレッド


スレッド
主に3つの関数、pthread_を使用します.create(作成)、pthread_exit(終了)、pthread_join(終了待ち).
例は次のとおりです.
/* example.c*/
#include <stdio.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.
"); } int main(void) { pthread_t id; int i,ret; ret=pthread_create(&id,NULL,(void *) thread,NULL); if(ret!=0){ printf ("Create pthread error!
"); exit (1); } for(i=0;i<3;i++) printf("This is the main process.
"); pthread_join(id,NULL); return (0); }

このプログラムをコンパイルします:gcc example 1.c-lpthread-o example 1はexample 1を実行し、This is the main processという結果を得た.This is a pthread. This is the main process. This is the main process. This is a pthread. This is a pthread. もう一度実行すると、This is a pthread.This is the main process. This is a pthread. This is the main process. This is a pthread. This is the main process.