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);}运行结果如我所预期的那样。

10gbiz首月半价月付2.36美元,香港/洛杉矶VPS、硅谷独立服务器/站群服务器

收到10gbiz发来的7月份优惠方案,中国香港、美国洛杉矶机房VPS主机4折优惠码,优惠后洛杉矶VPS月付2.36美元起,香港VPS月付2.75美元起。这是一家2020年成立的主机商,提供的产品包括独立服务器租用和VPS主机等,数据中心在美国洛杉矶、圣何塞和中国香港。商家VPS主机基于KVM架构,支持使用PayPal或者支付宝付款。洛杉矶VPS架构CPU内存硬盘带宽系统价格单核512MB10GB1...

杭州王小玉网-美国CERA 2核8G内存19.9元/月,香港,日本E3/16G/20M CN2带宽150元/月,美国宿主机1500元,国内宿主机1200元

官方网站:点击访问王小玉网络官网活动方案:买美国云服务器就选MF.0220.CN 实力 强 强 强!!!杭州王小玉网络 旗下 魔方资源池 “我亏本你引流活动 ” mf.0220.CNCPU型号内存硬盘美国CERA机房 E5 2696v2 2核心8G30G总硬盘1个独立IP19.9元/月 续费同价mf.0220.CN 购买湖北100G防御 E5 2690v2 4核心4G...

spinservers春节优惠:$149/月10Gbps圣何塞服务器-2*E5-2630Lv3 CPU,256G内存,2*1.6T SSD硬盘

spinservers是Majestic Hosting Solutions LLC旗下站点,商家提供国外服务器租用和Hybrid Dedicated等产品,数据中心包括美国达拉斯和圣何塞机房,机器默认10Gbps端口带宽,高配置硬件,支持使用PayPal、信用卡、支付宝或者微信等付款方式。农历春节之际,商家推出了几款特别促销配置,最低双路E5-2630Lv3机器每月149美元起,下面列出几款机器...

pthread_t为你推荐
svn服务器搭建如何在本机搭建SVN服务器小四号字word里的小四号字在Photoshop里是指多少点字体?awv如何把普通电影转换成AWV审计平台什么是审计工具文本框透明html怎么把这个文本框变为透明?kjavaJava与KJava有什么区别吗.?activitygroupActivityGroup子activity之间的切换效果怎么实现数据库界面数据库怎么进入界面微信网页版怎么看朋友圈电脑版微信怎么看朋友圈和发朋友圈点心os点心操作系统?
子域名查询 重庆服务器租用 godaddy主机 wordpress技巧 光棍节日志 win8.1企业版升级win10 web服务器架设 河南移动邮件系统 tna官网 中国电信测速网 个人免费主页 万网空间管理 登陆空间 广州虚拟主机 中国域名 畅行云 主机返佣 免备案jsp空间 美国代理服务器 web服务器 更多