网站开发工期安排,群晖 搭建两个wordpress,网站后台维护技能,亚马逊全球开店官方网站linux下线程分离属性 一、线程的属性---分离属性二、线程属性设置2.1 线程创建前设置分离属性2.2 线程创建后设置分离属性 一、线程的属性—分离属性
什么是分离属性#xff1f; 首先分离属性是线程的一个属性#xff0c;有了分离属性的线程#xff0c;不需要别的线程去接合… linux下线程分离属性 一、线程的属性---分离属性二、线程属性设置2.1 线程创建前设置分离属性2.2 线程创建后设置分离属性 一、线程的属性—分离属性
什么是分离属性 首先分离属性是线程的一个属性有了分离属性的线程不需要别的线程去接合自己的(回收自己的资源)。 a.线程的分离属性有两种分离态detached和非分离态joinable。
b.非分离态的线程只有在调用pthread_join之后才会完全释放自己所占用的资源可以用pthread_detach函数将线程修改为分离态分离态的线程在线程结束后由系统自动释放其所占用的所有资源 c.线程创建后默认属性是非分离态需要调用pthread_join释放线程所占用的资源
但是虽然说是分离的但是进程退出了该线程还是会退出的。 总结 设置了分离属性的线程 - 不需要pthread_join() 设置了非分离属性的线程 - 需要pthread_join() - 默认创建的普通属性线程就是非分离线程。 退出进程所有子线程都会消亡系统会回收他们资源。
如果设置为非分离属性的线程且不去回收子线程资源那么一个进程创建的子线程数据是有限请下面程序环境ubuntu20.04 64位
#include stdio.h
#include pthread.h
#include stdlib.h
#include unistd.h//定义退出的值为全局变量
int retval 10;//子线程
void *start_pthread(void *arg)
{int cnt 0;printf(arg:%ld\n, *((unsigned long *)arg));}int main(void)
{int cnt 0;unsigned long val 0;pthread_t thread_id;//主线程 运行时间比子线程时间长主线程运行退出后回收子线程资源while (1){//线程创建 非分离int ret pthread_create(thread_id, NULL, start_pthread, val);if(ret ! 0){printf(pthread_create fail\n);return -1;} val;}printf(接合子线程成功\n);return 0;}运行效果
如果不设置为分离属性主不回收子线程资源创建到32749条时创建线程失败
二、线程属性设置
线程属性设置通常有两个种一种是在创建前设置好分离属性别一个种是线程运行后再设置线程属性接下来先看第一种情况。
2.1 线程创建前设置分离属性
线程创建前设置分离属性就是添加一个分离属性到一个属性变量中然后使用属性变量去创建一个线程那么创建出来的线程就是具有分离属性的线程。 接下来看函数
1定义一个属性变量 - 数据类型pthread_attr_t
pthread_attr_t attr;2初始化属性变量。 - pthread_attr_init() - man 3 pthread_attr_init
#include pthread.h
int pthread_attr_init(pthread_attr_t *attr);参数 attr未初始化的属性变量 返回值 成功0 失败非0错误码 3设置分离属性到属性变量中。
#include pthread.h
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);参数 attr已经初始化过的属性变量 detachstate PTHREAD_CREATE_DETACHED - 分离属性 PTHREAD_CREATE_JOINABLE - 非分离属性 等同于 pthread_create(thread_id, NULL, start_pthread, val); 返回值 成功0 失败非0错误码 4使用属性变量去创建一个新的线程。
pthread_create(tid,attr,.....); - 创建出来的线程就是分离属性的线程不需要pthread_join()5销毁属性变量。 - pthread_attr_destroy() - man 3 pthread_attr_destroy
int pthread_attr_destroy(pthread_attr_t *attr);参数 attr已经初始化过的属性变量 返回值 成功0 失败非0错误码
案例代码
#include stdio.h
#include pthread.h
#include stdlib.h
#include unistd.h//定义退出的值为全局变量
int retval 10;//子线程有分离--不需要接合
void *start_pthread(void *arg)
{int cnt 0;printf(arg:%ld\n, *((unsigned long *)arg));}int main(void)
{int cnt 0;unsigned long val 0;pthread_t thread_id;//定义分享属性变量 pthread_attr_t attr;//初始化属性变量pthread_attr_init(attr);//设置线程属性--分离属性pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);//主线程 运行时间比子线程时间长主线程运行退出后回收子线程资源while (1){//线程创建 分离属性int ret pthread_create(thread_id, attr, start_pthread, val);if(ret ! 0){printf(pthread_create fail\n);return -1;} val;}printf(接合子线程成功\n);return 0;}分离属性后的线程可以自动消亡主线程可以无限创建子线程。
2.2 线程创建后设置分离属性
先创建一个普通线程(分享属性设置为NULL, 默认的分享属性为不分离)然后在线程中调用一个设置分离属性的函数那么这个线程就变成分离的属性。 1设置线程本身的属性为分离属性。 - pthread_detach() - man 3 pthread_detach
#include pthread.h
int pthread_detach(pthread_t thread);函数作用 设置分离属性给线程 参数 thread需要设置分离属性的线程的ID号 返回值 成功0 失败非0错误码 2获取线程的ID号。 - pthread_self() - man 3 pthread_self
#include pthread.h
pthread_t pthread_self(void);参数 无 返回值线程的ID号。
案例
#include stdio.h
#include pthread.h
#include stdlib.h
#include unistd.h//子线程
void *start_pthread(void *arg)
{int cnt 0;//获取线程IDpthread_t thread_id pthread_self();//分离线程int ret pthread_detach(thread_id);if(ret ! 0){printf( pthread_detach fail\n);exit(0);}printf(arg:%ld\n, *((unsigned long *)arg));}int main(void)
{unsigned long val 0;pthread_t thread_id;//主线程 运行时间比子线程时间长主线程运行退出后回收子线程资源while (1){//线程创建 默认为非分离int ret pthread_create(thread_id, NULL, start_pthread, val);if(ret ! 0){printf(pthread_create fail\n);return -1;} val;}return 0;}设置了分离属性之后子线程可以无限被创建。 文章转载自: http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.mymz.cn.gov.cn.mymz.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.dqkcn.cn.gov.cn.dqkcn.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.dodoking.cn.gov.cn.dodoking.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn