pthread多线程

来源:互联网 发布:linux telnet服务开启 编辑:程序博客网 时间:2024/05/18 01:19

int pthread_join(pthread_t thread, void **retval);

返回值:若成功则返回0,否则返回错误编号。

调用该函数的线程将一直阻塞,直到指定的线程thread调用pthread_exit,从启动例程中返回或者被取消。

看如下程序:

#include <stdio.h>#include <pthread.h>void* printMsg1(void *data){printf("the thread 1 id is %u\n",(unsigned int)pthread_self());sleep(5);}void* printMsg2(void *data){printf("the thread 2 id is %u\n",(unsigned int)pthread_self());}int main(){pthread_t t1,t2;pthread_create(&t1,NULL,printMsg1,NULL);pthread_join(t1,NULL);printf("I am waiting\n");pthread_create(&t2,NULL,printMsg2,NULL);pthread_join(t2,NULL);return 0;}
运行结果为:

tomcat@tomcat-Lenovo-IdeaPad-Y550:~/code$ ./pthreadTest
the thread 1 id is 2745190144
I am waiting
the thread 2 id is 2745190144


在t1执行完毕后,才打印出了I am waiting。说明程序在pthread_join处一直阻塞等待t1线程结束。

0 0
原创粉丝点击