电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

linux 多线程技术

25页
  • 卖家[上传人]:第***
  • 文档编号:54571039
  • 上传时间:2018-09-15
  • 文档格式:PPT
  • 文档大小:1.60MB
  • / 25 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、Linux 多线程技术,POSIX 线程库Pthreads,使用fork() 创建进程 代价昂贵 进程间通信方式较复杂 操作系统在实现进程间的切换比线程切换更费时,使用pthreads库创建线程 创建进程比创建线程更快 线程间的通信方式更容 操作系统对线程的切换比对进程的切换更容易和快速,线程的创建,#include int pthread_create(pthread_t * thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg ); 第一个参数为指向线程标识符的指针。 第二个参数用来设置线程属性。 第三个参数是线程运行函数的起始地址。 最后一个参数是运行函数的参数。 当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。,一个简单例子,#include #include #include #include #include pthread_t nti

      2、d; void *thr_fn(void *arg) printids(“new thread:“); return (void *)0); int main() int err; err = pthread_create( ,编译多线程程序,gcc -o mypthread -lpthread mypthread.c,线程的退出,调用pthread_exit()结束线程执行 void pthread_exit(void *retval); 让线程处理程序返回 使用 pthread_cancel() 函数终止其他线程的执行 int pthread_cancel(pthread_t thread); 向线程t发送取消请求,默认情况下线程thread自己调用pthread_exit(PTHREAD_CANCELED),,等待线程结束,使用 pthread_join() 函数等待被创建的线程结束 pthread_join() 函数会挂起创建线程的线程的执行 直到等待到想要等待的子线程 函数原型 : int pthread_join(pthread_t th, void *thread_ret

      3、urn);,线程的分离,主线程可以不断地创建子线程 子线程本身自己有自我回收内存资源的能力 函数原型: int pthread_detach(pthread_t th); pthread_detach() 和 pthread_join() 一般情况下不能同时使用,获得当前线程的标志,pthread_t pthread_self(void); 本函数返回本线程的标识符。 在LinuxThreads中,每个线程都用一个pthread_descr结构来描述,其中包含了线程状态、线程ID等所有需要的数据结构,此函数的实现就是在线程栈帧中找到本线程的pthread_descr结构,然后返回其中的p_tid项。,一个例子,#include #include #include #include #define THREAD_NUMBER 2 int retval_hello1= 2, retval_hello2 = 3; void* hello1(void *arg) char *hello_str = (char *)arg; sleep(1); printf(“%sn“, hello_str);

      4、 pthread_exit( ,void* hello2(void *arg) char *hello_str = (char *)arg; sleep(2); printf(“%sn“, hello_str); pthread_exit(,printf(“Begin to create threads.n“); ret_val = pthread_create( ,printf(“Begin to wait for threads.n“); for(i = 0; i THREAD_NUMBER; i+) ret_val = pthread_join(pti, (void *) ,线程属性的初始化和撤销,线程初始化: int pthread_attr_init(pthread_attr_t *attr) 初始化线程属性对象attr,并用默认值填充 线程撤销: int pthread_attr_destroy(pthread_attr_t *attr) 销毁线程属性对象attr。 *修改线程属性对象attr只有在线程创建前有效,在线程创建后修改对当前线程不起作用。 *返回0成功,否则失败

      5、。,pthread_attr_t定义,pthread_attr_t定义: typedef struct _pthread_attr_s int _detachstate; int _schedpolicy; struct _sched_param _schedparam; int _inheritsched; int _scope; size_t _guardsize; int _stackaddr_set; void *_stackaddr; size_t _stacksize; pthread_attr_t;,线程的属性,线程的属性(续),相关函数-分离状态,设置分离状态:pthread_attr_setdetachstate int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 返回值:函数成功返回0;任何其他返回值都表示错误 设置分离状态。 参数detachstate的值为:PTHREAD_CREATE_DETACHED、PTHREAD_CREATE_JOINABLE。 获取分离状态:pt

      6、hread_attr_getdetachstate int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate); 返回值:函数成功返回0;任何其他返回值都表示错误 取线程分离状态:分离的或是非分离的。,相关函数-调度策略,设置调度策略:pthread_attr_setschedpolicy int pthread_attr_setschedpolicy(pthread_attr_t *tattr, int policy); 返回值:函数成功返回0;任何其他返回值都表示错误 。 POSIX标准定义的调度策略有:SCHED_FIFO(先入先出)、SCHED_RR(循环)、SCHED_OTHER(由不同版本的POSIX线程库定义的缺省调度策略)。 获取调度策略:pthread_attr_getschedpolicy int pthread_attr_getschedpolicy(pthread_attr_t *tattr, int *policy); 返回值:函数成功返回0;任何其他返回值都表示错误 。,相关

      7、函数-调度参数,设置调度参数:pthread_attr_setschedparam int pthread_attr_setschedparam(pthread_attr_t *tattr, const struct sched_param *param); 返回值:函数成功返回0;任何其他返回值都表示错误 。 属性对象的调度参数定义在param结构中;在这个结构中只定义了优先级priority成员。新创建线程的优先级由属性对象中param结构的priority参数指定。 有两种方式可以修改线程的优先级。可以在创建子线程前设置属性对象的优先级参数;也可以先修改父线程的优先级,然后创建子线程。 sched_param结构中有可能存放着其他一些调度信息。所以在修改线程属性对象的调度参数前先取现有的调度参数是良好的习惯。 一段合理的代码应该是这样的:先取线程属性对象中现有的调度参数,对取出的调度参数进行操作,再用修改过的调度参数重置线程属性对象。 获取调度参数:pthread_attr_getschedparam int pthread_attr_getschedparam(pthread_

      8、attr_t *tattr, const struct sched_param *param); 返回值:函数成功返回0;任何其他返回值都表示错误 。,相关函数-域,设置域:pthread_attr_setscope int pthread_attr_setscope(pthread_attr_t *tattr, int scope); 返回值:函数成功返回0;任何其他返回值都表示错误 。 指定将来创建的线程是绑定(PTHREAD_SCOPE_SYSTEM)的还是非绑定的(PTHREAD_SCOPE_PROCESS)。 在一个进程中可以同时有这两种不同类型的线程。 获取域:pthread_attr_getscope int pthread_attr_getscope(pthread_attr_t *tattr, int *scope); 返回值:函数成功返回0;任何其他返回值都表示错误。,例子:threadattr,#include #include #include void* sum_val(void *arg) int sum = 0; int i; int count = *(int *)arg; for (i = 0; i count; i+) sum = sum + i; printf(“sum is %dn“, sum); pthread_exit(0); ,例子:threadattr(续),int main(int argc, char *argv) pthread_t pt; int count = 10; int ret_val; pthread_attr_t attr; struct sched_param sp; sp._sched_priority = 2; ret_val = pthread_attr_init( ,例子:threadattr(续),ret_val = pthread_attr_setdetachstate( ,例子:threadattr(续),ret_val = pthread_attr_setinheritsched( ,

      《linux 多线程技术》由会员第***分享,可在线阅读,更多相关《linux 多线程技术》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2 2022年小学体育教师学期工作总结
     
    收藏店铺
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.