int
pthread_join(pthread_t th, void **thread_return);
th: 指定了将要等待的线程标识符
thread_return: 它指向另外一个指针,而后者指向线程的返回值
成功时返回“0”,失败时返回一个错误代码
pthread_join相当于进程用来等待子进程的wait函数
作用:在线程结束后把它们归并到一起
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void *thread(void *str)
{
int i;
for (i = 0; i < 3; ++i)
{
sleep(2);
printf( "This in the thread : %dn" , i );
}
return NULL;
}
int main()
{
pthread_t pth;
int i;
/*创建线程并执行线程执行函数*/
int ret = pthread_create(&pth, NULL, thread, NULL);
printf("The main process will be to run,but will be blocked soonn");
/*阻塞等待线程退出*/
pthread_join(pth, NULL);
printf("thread was exitn");
for (i = 0; i < 3; ++i)
{
sleep(1);
printf( "This in the main : %dn" , i );
}
return 0;
}
阅读(890) | 评论(0) | 转发(0) |