#include<pthread.h>intpthread_create(pthread_t *thread,const pthread_attr_t *attr,void*(*start_routine)(void*),void*arg);Compile and link with -pthread.#include<pthread.h>#include<unistd.h>#include<stdio.h>void*func(void*arg){printf("I am a common thread, pid is %d, tid is %ld\n",getpid(),pthread_self());pthread_exit(NULL);}intmain(){pthread_t tid;pthread_create(&tid,NULL, func,NULL);printf("I am a man thread, pid is %d,create tid is %ld\n",getpid(), tid);printf("I am a man thread, pid is %d, tid is %ld\n",getpid(),pthread_self());pthread_exit(NULL);return0;}經測試,主線程使用pthread_exit函數,可以等待子線程的退出。
#include<pthread.h>#include<unistd.h>#include<stdio.h>void*func(void*arg){printf("I am a common thread, pid is %d, tid is %ld\n",getpid(),pthread_self());// pthread_exit((void *)100);return(void*)(100);}intmain(){pthread_t tid;pthread_create(&tid,NULL, func,NULL);printf("I am a man thread, pid is %d,create tid is %ld\n",getpid(), tid);printf("I am a man thread, pid is %d, tid is %ld\n",getpid(),pthread_self());void* ret;pthread_join((tid),&ret);printf("join tid return value is %d\n",(int)ret);return0;}