位置: 编程技术 - 正文

Linux 多线程编程实例(linux多线程编程电子书)

编辑:rootadmin

推荐整理分享Linux 多线程编程实例(linux多线程编程电子书),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:linux多线程编程电子书,linux多线程编程实验,linux多线程编程实例,linux多线程编程的书,linux 多线程编程解决资源竞争问题,linux多线程编程实验,linux多线程编程实例,linux 多线程编程解决资源竞争问题,内容如对您有帮助,希望把文章链接给更多的朋友!

一、多线程 VS 多进程

和进程相比,线程有很多优势。在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护代码段和数据。而运行于一个进程中的多个线程,他们之间使用相同的地址空间。正是这样,同一进程下的线程之间共享数据空间,数据可以相互使用,并且线程间切换也要更快些,可以更有效的利用CPU。

二、程序设计

[注] 头文件<pthread.h> 编译时要加载动态库 libpthread.a,使用 -lpthread 1、创建线程2、等待线程3、关闭线程4、退出清除 1、创建线程复制代码代码如下: int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, void *(*start_rtn)(void), void *arg)tidp为线程id,是函数分配的值,所以要传一个 pthread_t 的地址。attr线程属性,通常为空。start_rtn为线程要执行的函数,返回值为空指针,参数为后面的*arg若成功则返回0,否则返回出错编号。 例:

复制代码代码如下:#include<stdio.h>#include<pthread.h></p><p>void *func1(void *arg){ //原函数声明 int i; for(i=0;i<5;i++){ printf("this is func1! The num is %dn",*(int*)arg); //将空指针转换为int型指针 sleep(1); }}</p><p>void *func2(int *m){ //自定义类型声明,也可以定义非指针类型,但是在create时会有警告,因为非地址并不能改变传入的值 int i; for(i=0;i<5;i++){ printf("this is func2! The num is %dn",*m); (*m)++; sleep(1); }}</p><p>int main(){ pthread_t id1,id2; int num = 5; int *p = &num; if(pthread_create(&id1,NULL,(void *)func1,(void *)p) != 0){ printf("thread1 create error!n"); return -1; } if(pthread_create(&id2,NULL,(void *)func2,&num) != 0){ printf("thread2 create error!n"); return -1; } pthread_join(id1,NULL); //等待线程结束 pthread_join(id2,NULL); printf("Running complete!n"); return 0;</p><p>}

运行结果:

复制代码代码如下:[fsy@localhost process]$ gcc thC.c -o thC -lpthread -g[fsy@localhost process]$ ./thCthis is func2! The num is 5this is func1! The num is 6this is func2! The num is 6this is func1! The num is 7this is func2! The num is 7this is func1! The num is 8this is func2! The num is 8this is func1! The num is 9this is func2! The num is 9this is func1! The num is Running complete![fsy@localhost process]$

Linux 多线程编程实例(linux多线程编程电子书)

2、等待线程

[注]当调用pthread_create函数时,线程并没有开始执行,主进程应有等待,比如用sleep,或者用更专业的函数:pthread_join复制代码代码如下: int pthread_join(pthread_t tid, void **rval_ptr) 调用函数可以阻塞调用线程,直到指定的线程终止。tid为等待退出线程的id,rval_ptr为函数的返回值。是指向指针的指针,可以置空。 例:复制代码代码如下:#include<stdio.h>#include<pthread.h>#include<stdlib.h></p><p>void *func(int *p){ int *num=(int *)malloc(sizeof(int)); //必须动态创建,原因可以参考我动态分配内存的博客 printf("Please input the number: "); scanf("%d",num); return (void *)num; //类型是pthread_create的参数规定的}</p><p>int main(){ pthread_t pth; void *a; if(pthread_create(&pth,NULL,(void *)func,NULL) != 0){ printf("create thread error!n"); return 1; } pthread_join(pth,&a); //指向空指针的指针 printf("get the num from the thread, it's %dn",*(int *)a); return 0;}

3、终止线程

线程终止有以下三种方式:

1、线程从函数中返回2、线程可以别其他函数终止3、线程自己调用pthread_exit函数复制代码代码如下:void pthread_exit(void *rval_ptr)rval_ptr为线程退出返回值的指针,即函数返回值。

4、退出清除复制代码代码如下: void pthread_cleanup_push(void (*rtn)(void*), void *arg)rtn为清除函数,arg是清除函数的参数复制代码代码如下:void pthread_cleanup_pop(int execute)当execute 非0时执行清除函数。为0时不执行。

从pthread_cleanup_push的调用点到pthread_cleanup_pop之间的程序段中,如果有终止进程的动作,如调用pthread_exit或异常终止(不包括return),就会执行pthread_cleanup_push()所指定的清理函数。多个嵌套匹配时,就近匹配。 例:复制代码代码如下:#include<stdio.h>#include<pthread.h></p><p>void *clean(char *argv){ printf("clean is called by %sn",argv); return NULL;}void *func1(void *argv){ printf("welcome enter the func1!n"); pthread_cleanup_push((void*)clean,"the first time call!"); pthread_cleanup_push((void*)clean,"the second time call!"); if(argv){ return (void *)1; //第二次运行将此句注掉 } pthread_cleanup_pop(0); pthread_cleanup_pop(1); return (void *)0;}</p><p>void *func2(void *argv){ sleep(1); //两个线程运行先后不确定 printf("welcome enter the func2!n"); pthread_cleanup_push((void*)clean,"the first time call!"); pthread_cleanup_push((void*)clean,"the second time call!"); if(argv){ pthread_exit(NULL); } pthread_cleanup_pop(0); pthread_cleanup_pop(0); return (void *)0;}</p><p>int main(){ pthread_t tid1,tid2; if(pthread_create(&tid1,NULL,(void *)func1,(void *)1) != 0){ printf("thread1 create error!n"); return 1; }</p><p> if(pthread_create(&tid2,NULL,(void *)func2,(void *)1) != 0){ printf("thread2 create error!n"); return 1; } pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0;}

运行结果:复制代码代码如下:[fsy@localhost process]$ gcc thClean.c -o thclean -lpthread[fsy@localhost process]$ ./thcleanwelcome enter the func1!welcome enter the func2!clean is called by the second time call! //此处先2后1clean is called by the first time call![fsy@localhost process]$ vim thClean.c[fsy@localhost process]$ gcc thClean.c -o thclean -lpthread[fsy@localhost process]$ ./thcleanwelcome enter the func1!clean is called by the first time call! //second已经被popwelcome enter the func2!clean is called by the second time call!clean is called by the first time call![fsy@localhost process]$

Linux消息队列编程示例 一、概念消息队列就是一个消息的链表。可以把消息看作一个记录,具有特定的格式以及特定的优先级。对消息队列有写权限的进程可以向中按照一定

Percona Server 5.5.-.3/5.6.-.0 发布 PerconaServer5.5.-.3发布,此版本基于MySQL5.5.,包括其所有bug修复,是当前5.5系列最新的稳定版本。此版本现已提供下载,也提供在PerconaSoftwareRepositories

Linux iptables的备份和恢复操作详解 1、iptables的配置文件位置:/etc/sysconfig/iptables主控机:即业务运行中的机子;备份机:即主控机故障时,切换到备份的机子--在主控机上的操作-------------

标签: linux多线程编程电子书

本文链接地址:https://www.jiuchutong.com/biancheng/364796.html 转载请保留说明!

上一篇:Linux共享内存编程示例(linux共享内存同步机制)

下一篇:Linux消息队列编程示例(linux消息队列阻塞)

  • 税前扣除凭证按照用途分为哪些
  • 先申报财务报表还是企业所得税汇算清缴
  • 税务师考试2023年考试时间
  • 税收滞纳金是什么债权
  • 广交会展务
  • 公司账户短期理财收益要交税吗
  • 代扣代缴增值税纳税义务发生时间
  • 个体户的附加税表怎么填写
  • 出差飞机票报销标准
  • 软件工程质保金
  • 以前年度损益所得怎么算
  • 净利润增长率计算公式是什么意思
  • 个体工商户该如何交税
  • 培训费发票可以抵扣进项税吗
  • 出口企业增值税怎么算
  • 增值税开票系统收款人在哪里设置
  • 增值说税率怎么计算
  • 进项抵扣抵扣
  • 公司投资私募股票有哪些
  • 公积金是否计入个人所得税
  • 现金净流量正常值
  • 按照税收一般规定计算的折旧摊销金额
  • 广告公司进项税有哪些
  • 进口货物的企业有哪些
  • 税务自查补缴税款的申报表在哪里找
  • 合伙企业收到分红需要交所得税吗
  • 苹果微信朋友圈怎么发文字不带图片
  • 电脑进步了系统
  • php数组函数输出《咏雪》里有多少"片"字
  • 银行承兑汇票质押率90%
  • 开展党建系列活动
  • 消防工程改造方案
  • phpopendir
  • 非营利组织增值税
  • php制作微信小程序
  • 企业取得土地使用权可能作为什么核算
  • 多模态 融合
  • ntpd命令详解
  • 发票作废发票怎么处理
  • 个人所得税专项附加扣除标准一览表
  • 职工薪酬在利润表哪里看
  • 期初未分配利润在哪个表
  • 净资产怎么增长
  • vue$router
  • 织梦标签工具
  • php命令行模式
  • 个人转帐多少不引起注意
  • 小微企业增值税减免政策
  • 固定资产折旧计入什么费用
  • 企业缴纳的住房公积金的税率
  • 凭证字号和凭证编号一样吗
  • 一般纳税人公司开普票几个税点?
  • 来料加工的增值税怎么核算
  • 研发费用加计扣除的条件
  • 企业印花税率
  • 企业发放职工薪酬的账务处理
  • 个人取得的劳务报酬需要交增值税吗
  • 水果店的账务处理
  • 企业利润率如何计算公式
  • 固定资产暂估入账的税务处理
  • 计提利息为什么贷方是应付利息
  • 本年利润年末有余额怎么回事
  • mysql 5.7.33安装
  • sql语句压缩
  • 微软mission
  • win7系统摄像头打不开
  • win8.1官方
  • bios如何设置
  • cmdexe文件损坏
  • windows7的常用菜单
  • Mac通过AppleID登录窗口
  • Linux VPN 出现 807 错误的解决办法
  • 第一个闹钟
  • Nodejs+express+html5 实现拖拽上传
  • css3兼容性问题
  • vue.js如何使用
  • python中如何去除空格
  • jquery日期选择器
  • 定额发票是什么票据类型
  • 国家税务局扬州 袁霞
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设