多核程序设计Linux多线程编程_第1页
多核程序设计Linux多线程编程_第2页
多核程序设计Linux多线程编程_第3页
多核程序设计Linux多线程编程_第4页
多核程序设计Linux多线程编程_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux多线程编程,IEEE POSIX标准p1003.1c (Pthreads)为处理线程的一组c语言类型定义了API。在Linux中,线程通常被视为“轻量级进程”。Linux生成过程中使用的函数为fork()或vfork()。线程创建和管理Linux可以使用pthreads提供的APIs,它是POSIX中的线程库。使用Fork()创建进程和使用POSIX线程库的差异:具有使用fork()创建进程的特征。成本高昂且通常情况下,子进程需要复制父进程(如数据)的整个上下文。过程之间的通信方式更复杂,如如何使用管道、消息、共享内存等。操作系统实现进程间切换比线程切换需要更多的时间。使用POSIX

2、 pthreads库创建线程的能力:线程可以使用进程中存在的资源。进程中的变量使线程之间的通信更加容易,例如多个线程可以共享数据。操作系统之间的线程切换比进程切换更容易、更快。Linux多线程编程,线程的编写pthread线程库提供的编写线程的函数为pthread _ create()# include int pthread _ create(pthread _ t * thread,pthread)您可以在线程的结束线程的处理程序中显示pthread_exit()调用,以便线程处理程序返回,而不是结束线程执行或调用pthread_exit()。void pthread _ exit(voi

3、d * retval);除了Pthread_exit()函数外,还可以使当前调用pthread_exit()的线程按显示方式终止,线程可以使用pthread_cancel()函数终止其他线程的执行。int pthread _ cancel(pthread _ t thread);等待线程退出pthread_join()函数,然后等待创建线程的线程执行。Int pthread _ join (pthread _ t th,void * * thread _ return);线程的分离主线程生成子线程,子线程本身具有回收自身内存资源的功能。int pthread _ detach(pthread

4、_ t th);获取当前线程标志使用pthread_self()函数获取当前线程的标志。pthread_self()的返回值是当前线程的标志。pthread _ t pthread _ self(void);线程互斥和同步互斥、原子性。Mutex的锁定和解锁操作是原子的,当一个线程执行mutex操作时,另一个线程不能对同一mutex执行其他操作。单一性。拥有互斥锁的线程不能拥有此互斥锁,除非释放互斥锁。不忙的等待。等待mutex进入解锁状态的线程将打破操作系统等待此mutex的线程。POSIX线程库包含三种类型的mutex fast(fast)mutex一个线程锁定了mutex,其他线程必须等

5、待mutex多次解锁状态迭代mutex已拥有此mutex的线程执行解锁操作,才能获得mutex。但是,对于其他线程,如果在此mutex处于解锁状态错误检测mutex被线程锁定的情况下,另一个线程尝试锁定mutex,则锁定函数pthread_mutex_lock()函数将返回edededededlk值int pthread _ mutex _ lock(pthread _ mutex _ t * mutex);int pthread _ mutex _ try plock(pthread _ mutex _ t * mutex);int pthread _ mutex _ unlock(pthr

6、ead _ mutex _ t * mutex);int pthread _ mutex _ destroy(pthread _ mutex _ t * mutex);mutex pthread _ mutex _ t fast mutex=pthread _ mutex _ initializer初始化;pthread _ mutex _ t rec mutex=pthread _ recursive _ mutex _ initializer _ nppthread _ mutex _ t err chread,在线程中使用mutex的简单代码如下:pthread _ mutex _ t

7、mylockmy lock=pthread _ mutex _ initializer;Pthread_mutex_lock(,条件变量是线程的同步设备,线程之间的条件变量允许一个线程在执行过程中满足特定条件时向另一个线程发送信号,另一个线程可能处于暂挂状态,您可以等待条件满足后再继续执行下一个操作。pthread _ cond _ tcond=pthread _ cond _ initializer;int pthread _ cond _ init(pthread _ cond _ t * cond,pthread _ condattr _ t * cond _ attr);int pth

8、read _ cond _ signal(pthread _ cond _ t * cond);int pthread _ cond _ broadcast(pthread _ cond _ t * cond);int pthread _ cond _ wait(pthread _ cond _ t * cond,pthread _ mutex _ t * mutex);int pthread _ cond _ timedwait(pthread _ cond _ t * cond,pthread _ mutex _ t * mutex,const streed)int pthread _ c

9、ond _ destroy(pthread _ cond _ t * cond);、线程的取消,一个线程可以向另一个线程发送请求,从而结束另一个线程的执行。当一个线程响应取消请求时,它等同于执行pthread_exit(pthread_canceled)操作。许多POSIX线程库函数是取消点。撤消操作的POSIX pthreads库的函数为int pthread _ setcancelstate(int state,int * oldstate)。PTHREAD_CANCEL_ENABLE接受线程取消请求PTHREAD_CANCEL_DISABLE不接受线程取消请求int PTHREAD _ setcanceltype(int type,int *)变更回应取消请求的类型void pthread_testcancel(void)。测试并运行待定撤消。可以编写程序,以便在代码中显式检测撤消请求、清理实例和撤消线

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论