博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pthread_exit
阅读量:6259 次
发布时间:2019-06-22

本文共 1894 字,大约阅读时间需要 6 分钟。

pthread_exit:
  • By having main() explicitly call pthread_exit() as the last thing it does, main() will block and be kept alive to support the threads it created until they are done.

就是说,main函数中调用pthread_exit的时候,进程不会退出,所以main创建的线程就不会退出,但是main的局部变量存储的堆栈应该已经释放了。见如下代码:

/****************************************************************************** FILE: hello_arg3.c* DESCRIPTION:*   This "hello world" Pthreads program demonstrates an unsafe (incorrect)*   way to pass thread arguments at thread creation.  In this case, the*   argument variable is changed by the main thread as it creates new threads.* AUTHOR: Blaise Barney* LAST REVISED: 07/16/14******************************************************************************/#include 
#include
#include
#define NUM_THREADS 8void *PrintHello(void *threadid){ long taskid; sleep(1); taskid = *(long *)threadid; printf("Hello from thread %ld\n", taskid); pthread_exit(NULL);}int main(int argc, char *argv[]){pthread_t threads[NUM_THREADS];int rc;long t;for(t=0;t

输出:

Creating thread 0Creating thread 1Creating thread 2Creating thread 3Creating thread 4Creating thread 5Creating thread 6Creating thread 7Hello from thread 140737488348392Hello from thread 140737488348392Hello from thread 140737488348392Hello from thread 140737488348392Hello from thread 140737488348392Hello from thread 140737488348392Hello from thread 140737488348392Hello from thread 140737488348392

当我们在pthread_exit(NULL)的前面加sleep(10)的时候输出是:

Creating thread 0Creating thread 1Creating thread 2Creating thread 3Creating thread 4Creating thread 5Creating thread 6Creating thread 7Hello from thread 8Hello from thread 8Hello from thread 8Hello from thread 8Hello from thread 8Hello from thread 8Hello from thread 8Hello from thread 8

 另外需注意:void *类型表示没有指定类型的指针

参考资料:https://computing.llnl.gov/tutorials/pthreads/

转载地址:http://jjqsa.baihongyu.com/

你可能感兴趣的文章
Tensorflow快餐教程(1) - 30行代码搞定手写识别
查看>>
聊聊flink Table的Set Operations
查看>>
3.3 卷积神经网络进阶-Inception-mobile_net
查看>>
JS学习系列 06 - 变量对象
查看>>
Swift开发应用时如何更方便地使用颜色?
查看>>
ubuntu虚拟机设置静态ip(windows能够ping通ubuntu虚拟机)
查看>>
Redis高级特性介绍及实例分析
查看>>
Android的复选框的详细开发案例分析
查看>>
iOS FMDB数据库之增删改查使用
查看>>
EventBus源码解析
查看>>
Android中绘制简单几何图形和路径Path
查看>>
Internationalization(i18n) support in SAP CRM,UI5 and Hybris
查看>>
Xcode Debug调试汇总
查看>>
设计模式:再严谨的单例也尽量不要使用
查看>>
TiDB at 丰巢:尝鲜分布式数据库
查看>>
三篇文章了解 TiDB 技术内幕 —— 谈调度
查看>>
Next.js踩坑入门系列(六) —— 再次重构目录
查看>>
1. Context - React跨组件访问数据的利器
查看>>
Git常用操作、提交到GitHub等
查看>>
Android基础 四大组件之广播(Broadcast)
查看>>