




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Linux多线程编程线程同步lym54533442发布于 2008-4-30 | 704次阅读 字号: 大中小(网友评论8条)我要评论 04-30 Linux多线程同步mutex折叠 1. 初始化:在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化:对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER, 或者调用pthread_mutex_init.对于动态分配的互斥量, 在申请内存(malloc)之后, 通过pthread_mutex_init进行初始化, 并且在释放内存(free)前需要调用pthread_mutex_destroy.原型: int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); 头文件: 返回值: 成功则返回0, 出错则返回错误编号. 说明: 如果使用默认的属性初始化互斥量, 只需把attr设为NULL. 其他值在以后讲解.2. 互斥操作:对共享资源的访问, 要对互斥量进行加锁, 如果互斥量已经上了锁, 调用线程会阻塞, 直到互斥量被解锁. 在完成了对共享资源的访问后, 要对互斥量进行解锁.首先说一下加锁函数:头文件: 原型: int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); 返回值: 成功则返回0, 出错则返回错误编号. 说明: 具体说一下trylock函数, 这个函数是非阻塞调用模式, 也就是说, 如果互斥量没被锁住, trylock函数将把互斥量加锁, 并获得对共享资源的访问权限; 如果互斥量被锁住了, trylock函数将不会阻塞等待而直接返回EBUSY, 表示共享资源处于忙状态.再说一下解所函数:头文件: 原型: int pthread_mutex_unlock(pthread_mutex_t *mutex); 返回值: 成功则返回0, 出错则返回错误编号.3. 死锁:死锁主要发生在有多个依赖锁存在时, 会在一个线程试图以与另一个线程相反顺序锁住互斥量时发生. 如何避免死锁是使用互斥量应该格外注意的东西. 总体来讲, 有几个不成文的基本原则:对共享资源操作前一定要获得锁. 完成操作以后一定要释放锁. 尽量短时间地占用锁. 如果有多锁, 如获得顺序是ABC连环扣, 释放顺序也应该是ABC. 线程错误返回时应该释放它所获得的锁.谈了这么多就让我举个实际点的例子来说明以上函数的功能:(代码来源于:GPRS服务器Linux编程作者:李杨明)#include #include #include #include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int lock_var;time_t end_time;int sum;void pthread1(void *arg);void pthread2(void *arg);void pthread3(void *arg);int main(int argc, char *argv)pthread_t id1,id2,id3;pthread_t mon_th_id;int ret;sum=10;end_time = time(NULL)+10;pthread_mutex_init(&mutex,NULL);ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);if(ret!=0)perror(pthread cread1);ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);if(ret!=0)perror(pthread cread2);ret=pthread_create(&id3,NULL,(void *)pthread3, NULL);if(ret!=0)perror(pthread cread3);pthread_join(id1,NULL);pthread_join(id2,NULL);pthread_join(id3,NULL);exit(0);void pthread1(void *arg)int i;while(time(NULL) end_time)if(pthread_mutex_lock(&mutex)!=0) /lockperror(pthread_mutex_lock);elseprintf(pthread1:pthread1 lock the variablen);for(i=0;i2;i+)sleep(2);lock_var+;if(pthread_mutex_unlock(&mutex)!=0) /unlockperror(pthread_mutex_unlock);elseprintf(pthread1:pthread1 unlock the variablen);sleep(1);void pthread2(void *arg)int nolock=0;int ret;while(time(NULL) end_time)ret=pthread_mutex_trylock(&mutex);/try lockif(ret=EBUSY)printf(pthread2:the variable is locked by pthread1n);elseif(ret!=0)perror(pthread_mutex_trylock);exit(1);elseprintf(pthread2:pthread2 got lock.The variable is %dn,lock_var);if(pthread_mutex_unlock(&mutex)!=0)/unlockperror(pthread_mutex_unlock);elseprintf(pthread2:pthread2 unlock the variablen);sleep(1);void pthread3(void *arg)/*int nolock=0;int ret;while(time(NULL) end_time)ret=pthread_mutex_trylock(&mutex);if(ret=EBUSY)printf(pthread3:the variable is locked by pthread1 or 2n);elseif(ret!=0)perror(pthread_mutex_trylock);exit(1);elseprintf(pthread3:pthread3 got lock.T
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论