多线程编程(2)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

多线程编程•基本概念–线程:进程中的某一个处理流程–一个进程可以有多个线程,进程是线程的父进程–所有线程与父进程共享资源多线程编程•线程分类–内核态线程•由内核调度程序直接调度,充分发挥多处理器的优势•目前linux系统标准线程库采用内核线程方式实现多线程–用户态线程•一个进程包含多个线程,这些线程从内核调度角度来看只是一个进程,内核把它当一个进程来调度。线程之间调度在用户态进行•用户态线程优点是调度效率高(不需要内核参与调度),缺点是对于多核处理器利用率不高,一个线程阻塞会导致整个线程组阻塞多线程编程•创建线程–#includepthread.h–intpthread_create(pthread_t*id,pthread_attr_t*attr,void*pFun,void*args)•id:返回线程ID•attr:线程属性•pFun:线程调用的函数•args:线程函数的参数多线程编程•退出线程方式–1:线程指定函数执行完毕–2:进程退出–3:线程调用exec函数–4:线程调用pthread_exit函数退出–5:线程调用pthread_cancel终止–pthread_exit(void*pRet)pRet指定线程退出返回值多线程编程•线程管理–intpthread_join(pthread_tid,void**pRet)–使主进程等待线程完成后才退出–pRet获得pthread_exit函数调用的返回值多线程编程•线程管理–pthread_self()•线程函数里,获取本线程的线程ID–pthread_getattr_np(pthread_tid,pthread_attr_t*attr)•获取线程属性多线程编程•线程属性,调用init函数初始化线程属性–pthread_attr_init(pthread_attr_t*attr)•structpthread_attr_t{•int__detachstate;•int__schedpolicy;•structsched_param__schedparam;•int__inheritsched;•int__scope;•size_t__guardsize;•int__stackaddr_set;•void*__stackaddr;•unsignedlongint__stacksize;•}多线程编程•线程属性–__detachstate:线程分离状态•PTHREAD_CREATE_JOINABLE可与其他线程连接•PTHREAD_CREATE_DETACHED–设置/获取线程分离状态•intpthread_attr_setdetachstate(pthread_attr_t*attr,intdetachstate)•intpthread_attr_getdetachstate(pthread_attr_t*attr,int*pdetachstate)多线程编程•线程属性–__schedpolicy:线程调度策略•SCHED_OTHER系统默认(分时调度),•各个优先级任务时间轮换•SCHED_FIFO实时调度,先到先服务(独占)•SCHED_RR实时调度,时间片轮转(高优先级轮换)–设置/获取线程调度策略•intpthread_attr_setschedpolicy(pthread_attr_t*attr,intpolicy)•intpthread_attr_getschedpolicy(pthread_attr_t*attr,int*pPolicy)多线程编程•线程属性–__schedparam线程优先级信息•__schedparam.sched_priority–设置获取线程属性参数•intpthread_attr_setschedparam(pthread_attr_t*attr,structsched_param*param)•intpthread_attr_getschedparam(pthread_attr_t*attr,structsched_param*param)•对于SCHED_FIFOSCHED_RR调度,设置优先级param.sched_priority多线程编程•线程属性–__inheritsched线程继承性•PTHREAD_INHERIT_SCHED从父进程继承调度属性•PTHREAD_EXPLICIT_SCHED不从父进程继承调度属性–设置/获取线程继承性–intpthread_attr_setinheritsched(pthread_attr_t*attr,intinheritsched)–intpthread_attr_getinheritsched(pthread_attr_t*attr,int*pinheritsched)多线程编程•线程属性–__scope线程作用域•PTHREAD_SCOPE_SYSTEM系统所有进程间调度•PTHREAD_SCOPE_PROCESS当前进程间调度–设置/获取线程作用域–intpthread_attr_setscope(pthread_attr_t*attr,intscope)–intpthread_attr_getscope(pthread_attr_t*attr,int*pscope)多线程编程•线程属性–__stackaddr线程堆栈地址–__stacksize线程堆栈大小–获取/设置线程桟•intpthread_attr_setstackaddr(pthread_attr_t*attr,void*stackaddr)•intpthread_attr_getstackaddr(pthread_attr_t*attr,void**stackaddr)•intpthread_attr_setstacksize(pthread_attr_t*attr,size_t*stacksize)•intpthread_attr_setstacksize(pthread_attr_t*attr,size_t*stacksize)多线程编程•线程属性–__guardsize警戒缓冲区大小•线程桟末尾防止桟溢出的扩展内存大小–设置/获取线程警戒缓冲区•intpthread_attr_getguardsize(pthread_attr_t*attr,size_t*guardsize)•intpthread_attr_setguardsize(pthread_attr_t*attr,size_t*guardsize)多线程编程•线程属性–pthread_attr_destroy(pthread_attr_t*attr)•无效化线程属性•无效化后,使用attr创建线程会失败多线程编程多线程编程•线程互斥–多线程共享进程资源,访问共享资源时候,需要进行互斥操作确保数据的有效性多线程编程•互斥锁类型–mutex锁类型PTHREAD_MUTEX_XXX•NORMAL:普通锁,不提供死锁检测,可能出现死锁•ERRORCHECK:检错锁,同一线程对已锁的互斥锁加锁会返回错误•RECURSIVE:嵌套锁,允许同一线程多次锁定而不产生死锁,多次释放才能被别的线程锁定•DEFAULT:默认为普通锁,排队获取锁多线程编程•锁类型设置/获取–intpthread_mutexattr_settype(pthread_mutexattr_t*attr,inttype);–intpthread_mutexattr_gettype(pthread_mutexattr_t*attr,int*type);多线程编程•线程互斥–创建互斥锁•pthread_mutex_init(pthread_mutex_t*mutex,pthread_mutexattr_t*attr)–互斥锁加锁•pthread_mutex_lock(pthread_mutex_t*mutex)–互斥锁解锁•pthread_mutex_unlock(pthread_mutex_t*mutex)多线程编程•线程互斥–pthread_mutex_*函数族man无显示解决办法•$sudoapt-getinstallmanpages-posix-dev多线程编程•创建互斥锁–pthread_mutex_init(pthread_mutex_t*mutex,pthread_mutexattr_t*attr)–attr==NULL初始化的线程锁mutex–等价于mutex=PTHREAD_MUTEX_INITIALIZER多线程编程•互斥锁加锁–pthread_mutex_lock(pthread_mutex_t*mutex)•对互斥锁加锁•同一线程内对已经加锁的互斥锁,进行再次加锁,会造成死锁多线程编程•互斥锁加锁–pthread_mutex_trylock(pthread_mutex_t*mutex)•试图锁住互斥体,不阻塞•1:互斥体未上锁,锁定并返回成功•2:互斥体已经上锁,不阻塞直接返回失败•3:锁类型为嵌套锁RECURSIVE,已上锁,则锁计数器自加多线程编程•互斥锁解锁–pthread_mutex_unlock(pthread_mutex_t*mutex)•释放互斥锁•对未锁定的互斥锁,进行释放操作,会产生不确定的行为•尝试解除有其他线程创建的互斥锁,会产生不确定的行为多线程编程

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功