图形学优化_1: pthread

来源:互联网 发布:淘宝网店上传宝贝教程 编辑:程序博客网 时间:2024/05/20 20:21

转自:http://blog.csdn.net/phunxm/article/details/5812836


线程的基本概念

  • 线程是进程的一个独立控制流
  • 线程:程序计数器,一组寄存器,栈,线程信号掩码,局部线程变量,线程私有数据
  • 线程和进程区别:资源共享
  • 线程间共享:地址空间,信号处理机制,数据,I/O
功能 线程 进程 创建 pthread_create fork,vfork 退出 pthread_exit exit 等待 pthread_join wait,waitpid 取消/终止 pthread_cancel abort 读取ID pthread_self() getpid 调度策略 SCHD_FIFO等 SCHD_FIFO等 通信机制 信号量,信号,互斥锁,变量条件,读写锁 无名管道,有名管道,信号,信息队列,信号量,共享内存
  • 线程拥有资源
资源 程序计数器 一组寄存器 栈 线程信号编码 局部线程变量 线程私有数据

创建线程

extern int pthread_create(pthread_t *_restrict_newthread,  //存储线程的ID_const pthread_attr)t *_restrict _attr, //线程的属性void *(*_start_rountine) (void*), //线程运行的代码起始地址void *_restrict_arg //运行函数的参数地址,若自定义函数需要多个参数,则需要用一个包含这些参数的结构体地址 )//成功返回0,失败返回1;
struct message{   int i;   int j;};void *hello(struct message *str){     printf("the arg.i is",str->i);      printf("the arg.j is",str->j);}int main(){    struct message test;    pthread_t thread_id;    test.i=10;    test.j=20;pthread_create(&thread_id,NULL,(void*)*hello,&test); //printf("the new thread id is %u\n",thread_id);pthread_join(thread_id,NULL); //在主程序中等待子线程结束}

线程的退出和等待

  • 线程的退出
extern void pthread_exit(void *_retval); 
  • 线程的等待
//为了有效的利用资源会等待线程的结束extern int pthread_join(pthread_t _th, //被等待的线程的IDvoid  **_thread_return  //用户定义的指针,保存线程退出后的返回值);

线程退出前的操作


线程的取消

  • 线程取消满足条件:(1)线程是否可以被取消是可设置;(2)线程处于可取消点
extern int pthread_cancel(pthread_t_cancelthread); //取消执行的线


线程的调度策略

  • FIFO:首先请求服务的对象首先得到cpu的处理
  • 最短作业优先,需要最小系统时间的服务首先得到处理
  • 最高优先级,优先级最高的得到处理
  • 时间片
* SCHED_OTHER:系统默认* SCHED_FIFO* SCHED_RR:轮转调动(类似于FIFO,加上时间轮片策略)


线程的同步机制

互斥锁

  • 互斥排他,防止数据被修改;开锁(0),上锁(1)
  • 访问资源前,首先申请该互斥锁。若处于开锁状态,占有该锁,申请对象;互斥锁变为锁定状态
  • 只有锁定该互斥锁的进程才能释放该互斥锁
初始化互斥锁------pthread_mutex_init阻塞申请互斥锁----pthread_mutex_lock释放申请互斥锁----pthread_mutex_unlock非阻塞申请互斥锁--pthread_mutex_trylock销毁互斥锁-------pthread_mutex_destroy

互斥锁的初始化和销毁

  • 定义互斥锁
pthread_mutex_t lock;
  • 初始化
extern int pthread_mutex_init(arg1,arg2);
  • 申请互斥锁
pthread_mutex_lock //阻塞申请pthread_mutex_trylock //非阻塞申请
  • 释放互斥锁
pthread_mutex_unlock
//例程:两个线程,一个线程负责从标准输入设备中读取数据存储在全局数据区,一个线程负责将读入的数据输出到标准的输出设备#include ......void *thread_function(void *arg);pthread_mutex_t work_mutex;  //全局互斥锁对象char work_area[1024]; //全局共享数据区int time_to_exit=0;int  main{   pthread_t a_thread;   pthread_mutex_init(&work_mutex,NULL); //初始化互斥锁   pthread_create(&a_thread,NULL,thread_function,NULL);  // 创建新线程   pthread_mutex_lock(&work_mutex); //接受输入前,给互斥锁上锁   .....函数操作   pthread_mutex_unlock(&work_mutex); //解锁   prthead_join(); //等待另一个线程结束   prthead_mutex_destroy(); // 销毁互斥锁} 

变量条件通信机制

  • 互斥锁的缺陷
  • 配合互斥锁使用

读写锁

只容许一个写入,可以多个读取
* 如果某线程申请了读锁,其他线程可以申请读锁,不可以申请写锁
* 如果其他线程申请了写锁,其他线程不能申请读锁,也不能申请写锁

pthread_rwlock_t rwlock; //全局变量

信号

线程信号掩码



防止死锁

死锁的原因

(1) 因为系统资源不足。
(2) 进程运行推进的顺序不合适。
(3) 资源分配不当等。

产生死锁的四个必要条件:

(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。

0 0
原创粉丝点击