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

柚子互联(34元),湖北十堰高防, 香港 1核1G 5M

柚子互联官网商家介绍柚子互联(www.19vps.cn)本次给大家带来了盛夏促销活动,本次推出的活动是湖北十堰高防产品,这次老板也人狠话不多丢了一个6.5折优惠券而且还是续费同价,稳撸。喜欢的朋友可以看看下面的活动详情介绍,自从站长这么久以来柚子互联从19年开始算是老商家了。六五折优惠码:6kfUGl07活动截止时间:2021年9月30日客服QQ:207781983本次仅推荐部分套餐,更多套餐可进...

百纵科技(19元/月),美国洛杉矶10G防御服务器/洛杉矶C3机房 带金盾高防

百纵科技官网:https://www.baizon.cn/百纵科技:美国云服务器活动重磅来袭,洛杉矶C3机房 带金盾高防,会员后台可自助管理防火墙,添加黑白名单 CC策略开启低中高.CPU全系列E52680v3 DDR4内存 三星固态盘列阵。另有高防清洗!美国洛杉矶 CN2 云服务器CPU内存带宽数据盘防御价格1H1G10M10G10G19元/月 购买地址2H1G10M10G10G29元/月 购买...

iON Cloud七月促销适合稳定不折腾的用户,云服务器新购半年付8.5折,洛杉矶/圣何塞CN2 GT线路,可选Windows系统

iON Cloud怎么样?iON Cloud今天发布了7月份优惠,使用优惠码:VC4VF8RHFL,新购指定型号VPS半年付或以上可享八五折!iON的云服务器包括美国洛杉矶、美国圣何塞(包含了优化线路、CN2 GIA线路)、新加坡(CN2 GIA线路、PCCW线路、移动CMI线路)这几个机房或者线路可供选择,有Linux和Windows系统之分,整体来说针对中国的优化是非常明显的,机器稳定可靠,比...

pthread_t为你推荐
李智慧韩国艺人韩智慧最近的情况?Honeypotnc如何使用cpu监控win10自带cpu温度监控数据监测运动手表的数据监测都准确吗?华为总裁女儿为啥姓孟孟姜女为什么不姓孟?oa办公系统下载免费oa办公软件哪里可以下载到?拜托各位大神vga接口定义VGA接口通常用来连接哪些设备,各个脚代表什么意思,它的连线是如何焊接的?数据统计分析表如何用Excel做数据分析?asp大马黑帽seo的webshell中,什么是大马和小马天翼校园宽带天翼校园宽带怎么样用手机打开这个页面登陆
重庆虚拟主机 .cn域名注册 域名查询软件 最新代理服务器地址 免费申请域名 免费顶级域名 ion paypal认证 godaddy续费优惠码 华为4核 河南m值兑换 php空间推荐 卡巴斯基是免费的吗 全能空间 国外代理服务器 新疆服务器 此网页包含的内容将不使用安全的https 美国vpn服务器 paypal登陆 cc攻击 更多