pthread_t[结贴]用pthread_create创建的pthread_t,需要显示的销毁么?

pthread_t  时间:2021-06-13  阅读:()

如何使用pthread

怎样知道一个pthread 如果需要只终止某个线程而不终止整个进程,可以有三种方法: 1.从线程函数return。

这种方法对主线程不适用,从main函数return相当于调用exit。

2.一个线程可以调用pthread_cancel终止同一进程中的另一个线程。

3.线程可以调用pthread_exit终止自己。

线程函数中,假如没有用pthread_exit会怎样?

首先必须搞清楚pthread_exit是linux线程库POSIX底下的一个结束线程(自身)的函数,在多线程环境中,pthread_exit只会清理本线程的资源,不会影响其他并行的线程,而exit(0),return(0)则不同,具体可见下程序,linux下编译: #include <stdio.h> #include <pthread.h> #include <stdlib.h> void thread(void) { int i; for(i=0;i<3;i++) printf("This is a pthread. "); pthread_exit(0); //可以不要,会自动调用 } int main(void) { pthread_t id; int i,ret; ret=pthread_create(&id,NULL,(void *) thread,NULL); if(ret!=0){ printf ("Create pthread error! "); exit (1); } for(i=0;i<3;i++) printf("This is the main process. "); pthread_join(id,NULL);//阻塞主线程,等待id线程结束。

return (0); //结束本进程内所有线程 }

怎么把pthread_mutex_t设为递归锁?

在初使化mutex时设置: pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mutex,&attr);

[结贴]用pthread_create创建的pthread_t,需要显示的销毁么?

谢谢,按照你说的,做了修改。

新的代码如下:#include #include #include #include #include using namespace std;void exe_exit1(void *arg){ printf("%s:arg=%s ",__FUNCTION__,(char *)arg);}void exe_exit2(void *arg){ printf("%s:arg=%s ",__FUNCTION__,(char *)arg);}void *func(void*){ printf("thread start "); pthread_cleanup_push(exe_exit2,static_cast(const_cast("For exe_exit1"))); pthread_cleanup_push(exe_exit1,static_cast(const_cast("For exe_exit2"))); printf("pthread_cleanup_push ok "); printf("pthread_cleanup_pop begin "); pthread_cleanup_pop(1); pthread_cleanup_pop(1); return NULL;}int main(){ pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); pthread_create(&tid,&attr,func,NULL); pthread_attr_destroy(&attr); sleep(1); exit(0);}运行结果如我所预期的那样。

VoLLcloud7折月付$3,香港CMI云服务器原生IP解锁,香港VoLLcloud

vollcloud怎么样?vollcloud LLC创立于2020年,是一家以互联网基础业务服务为主的 技术型企业,运营全球数据中心业务。VoLLcloud LLC针对新老用户推出全场年付产品7折促销优惠,共30个,机会难得,所有产品支持3日内无条件退款,同时提供产品免费体验。目前所有产品中,“镇店之宝”产品性价比高,适用大部分用户基础应用,卖的也是最好,同时,在这里感谢新老用户的支持和信任,我们...

1C2G5M轻量服务器48元/年,2C4G8M三年仅198元,COM域名首年1元起

腾讯云双十一活动已于今天正式开启了,多重优惠享不停,首购服务器低至0.4折,比如1C2G5M轻量应用服务器仅48元/年起,2C4G8M也仅70元/年起;个人及企业用户还可以一键领取3500-7000元满减券,用于支付新购、续费、升级等各项账单;企业用户还可以以首年1年的价格注册.COM域名。活动页面:https://cloud.tencent.com/act/double11我们分享的信息仍然以秒...

特网云-新上线香港五区补货资源充足限时抢 虚拟主机6折,低至38元!

官方网站:点击访问特网云官网活动方案:===========================香港云限时购==============================支持Linux和Windows操作系统,配置都是可以自选的,非常的灵活,宽带充足新老客户活动期间新购活动款产品都可以享受续费折扣(只限在活动期间购买活动款产品才可享受续费折扣 优惠码:AADE01),购买折扣与续费折扣不叠加,都是在原价...

pthread_t为你推荐
迅雷地址转换网页上的迅雷下载功能是怎么实现的,难道是用链接转换工具把普通下载地址转换成迅雷下载地址?谢谢 谢谢以图搜人怎样人肉搜人?只知道他名字和当初居住的地址视频技术视频制作有前途吗华为总裁女儿为啥姓孟孟姜女为什么不姓孟?star413CONVERSE和ALLSTAR有什么区别熊猫烧香病毒下载谁知道熊猫烧香病毒xcelsius谁有水晶易表2008的密钥?bt代理为什么用代理下载BT非常非常慢啊?微信网页版怎么看朋友圈电脑版微信怎么看朋友圈和发朋友圈监控插件常见的监控软件有哪些
香港vps主机 最新代理服务器ip 什么是域名解析 域名抢注工具 Dedicated 服务器日志分析 台湾谷歌网址 太原联通测速平台 坐公交投2700元 京东商城0元抢购 idc资讯 网站木马检测工具 域名评估 空间技术网 酷番云 支持外链的相册 新睿云 视频服务器是什么 河南移动梦网 阿里云免费邮箱 更多