多线程二pthread_exit和join
来源:互联网 发布:mac破解unity5.3失败 编辑:程序博客网 时间:2024/05/23 01:58
http://blog.sina.com.cn/s/blog_6572515e0100y4kt.html
如果进程中任何一个线程中调用exit,_Exit,或者是_exit,那么整个进程就会终止,与此类似,如果信号的默认的动作是终止进程,那么,把该信号发送到线程会终止进程。线程的正常退出的方式:
#include <pthread.h>
void pthread_exit(void *rval_ptr);
int pthread_join(pthread_t thread,void **rval_ptr);
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *create(void *arg)
{
}
int main(int argc,char *argv[])
{
}
执行结果:
new thread is created ...
thread is exit code 8
可以看出来,线程退出可以返回线程的int数值。线程退出不仅仅可以返回线程的int数值,还可以返回一个复杂的数据结构。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
struct menber
{
}temp={8,"zieckey"};
void *create(void *arg)
{
}
int main(int argc,char *argv[])
{
}
执行结果:
main ...
new thread ...
c->a = 8
c->b = zieckey
例程总结:
一定要记得返回的数据结构要是在这个数据要返回的结构没有释放的时候应用,如果数据结构已经发生变化,那返回的就不会是我们所需要的,而是脏数据。
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void *thread(void *str)
{
}
int main()
{
}
"pthread_join(pth, NULL);"这一行注释掉:
[root@localhost src]# gcc pthread_join.c -lpthread
[root@localhost src]# ./a.out
This in the main : 0
This in the thread : 0
This in the main : 1
This in the main : 2
This in the thread : 1
This in the main : 3
This in the main : 4
This in the thread : 2
This in the main : 5
This in the main : 6
This in the thread : 3
This in the main : 7
This in the main : 8
This in the thread : 4
This in the main : 9
子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了。
“pthread_join(pth, NULL);”起作用
[root@localhost src]# gcc pthread_join.c -lpthread
[root@localhost src]# ./a.out
This in the thread : 0
This in the thread : 1
This in the thread : 2
This in the thread : 3
This in the thread : 4
This in the thread : 5
This in the thread : 6
This in the thread : 7
This in the thread : 8
This in the thread : 9
This in the main : 0
This in the main : 1
This in the main : 2
This in the main : 3
This in the main : 4
This in the main : 5
This in the main : 6
This in the main : 7
This in the main : 8
This in the main : 9
[root@localhost src]#
这说明pthread_join函数的调用者在等待子线程退出后才继续执行
- 多线程二pthread_exit和join
- 多线程:线程的返回retrun和pthread_exit
- linux多线程问题:pthread_join 和 pthread_exit 的区别
- 多线程:pthread_exit,pthread_join,pthread_self
- 多线程CountDownLatch和Join
- pthread_exit()和return()
- java多线程join()和join(long millis)
- pthread_exit()
- pthread_exit
- pthread_exit
- pthread_exit()
- pthread_exit()
- pthread_exit()
- pthread_exit
- pthread_exit()
- pthread_exit()
- 多线程(join和yield)
- Linux线程pthread_exit 和 pthread_join
- Linux中find常见用法示例
- UBUNTU Eclipse 关于创建AVD时点击 OK 无效无反映解决(实在不行用命令行)
- tcp 头部选项的机制
- 从多核到众核:把困难推给软件
- 【shell】获取函数返回值的两种方法
- 多线程二pthread_exit和join
- 一个老兵的linux学习和面试经验分享
- 磁珠的作用
- 深度访谈:智能家居这么火,该怎么赚钱?
- javadoc学习经验分享
- GO-HOME我要回家JAVA2013版火车票抢票程序
- 一道金蝶校园招聘试题数据库笔试题
- Cocos2d-x学习之路---1
- nslog