linux用互斥量进行同步
来源:互联网 发布:炉石传说淘宝金币号 编辑:程序博客网 时间:2024/05/08 02:13
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <semaphore.h>void *thread_function(void *arg);pthread_mutex_t work_mutex; #define WORK_SIZE 1024char work_area[WORK_SIZE];int time_to_exit=0;int main(){ int res; pthread_t a_thread; void *thread_result; res=pthread_mutex_init(&work_mutex,NULL); if(res!=0) { perror("Mutex initialization failed"); exit(EXIT_FAILURE); } res=pthread_create(&a_thread,NULL,thread_function,NULL); if(res!=0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } pthread_mutex_lock(&work_mutex); printf("Input some text, Enter 'end' to finish\n"); while(!time_to_exit) { fgets(work_area,WORK_SIZE,stdin); pthread_mutex_unlock(&work_mutex); while(1) { pthread_mutex_lock(&work_mutex); if(work_area[0]!='\0') { pthread_mutex_unlock(&work_mutex); sleep(1); } else break; } } pthread_mutex_unlock(&work_mutex); printf("\nwaiting for thread to finish...\n"); res=pthread_join(a_thread,&thread_result); if(res!=0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); pthread_mutex_destroy(&work_mutex); exit(EXIT_SUCCESS);}void *thread_function(void *arg){ sleep(1); pthread_mutex_lock(&work_mutex); while(strncmp("end",work_area,3)!=0) { printf("You input %d characters\n",strlen(work_area)-1); work_area[0]='\0'; pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); while(work_area[0]=='\0') { pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); } } time_to_exit=1; work_area[0]='\0'; pthread_mutex_unlock(&work_mutex); pthread_exit(0);}
在程序的开始,我们声明了一个互斥量,工作区和一个变量time_to_exit。
然后初始化互斥量,接下来启动新线程,新线程首先试图对互斥量加锁,如果它已经被锁住,这个调用将被阻塞直到它被释放为止,一旦获得访问权,我们就检查是否有申请退出程序的请求,如果有就设置time_to_exit变量,再把工作区的第一个字符设置为\0,然后退出。
如果不想退出,就统计字符个数,然后把work_area数组中的第一个字符设置为NULL,我们用将第一个字符设置为NULL的方法通知读取输入的线程,我们已完成了字符统计,然后解锁互斥量并等待主线程继续运行。我们将周期性的尝试给互斥量加锁,如果加锁成功,就检查是否主线程又有字符送来要处理,如果还没有就解锁互斥量继续等待,如果有,就统计字符个数并再次进入循环。
这种通过轮询来获得结果的方法通常并不是好的编程方式,在实际的编程中,我们应该尽量用信号量来避免出现这种情况。
- linux用互斥量进行同步
- Linux程序设计--12.5.2用互斥量进行同步
- linux用信号量进行同步
- linux中进行时间同步
- linux程序设计——用互斥量进行同步(第十二章)
- linux 多线程用信号量进行同步例子
- 在linux下使用rsync进行同步
- Linux下使用ntpdate进行时间同步
- linux 下使用 rsync 进行文件 同步
- linux 下使用 rsync 进行文件 同步
- Linux使用rsync客户端与服务端同步目录进行备份
- linux使用rsync客户端与服务端同步目录进行备份
- NTP for Windows,与Linux进行时间同步
- Linux 利用 expect 进行 scp 自动同步文件
- 用互斥线程进行同步
- Rsync进行数据同步
- 如何进行数据同步
- 使用rsync进行同步
- 基础知识二
- 如何进行外汇市场的基础分析
- StringBuilder效率比String高的原因。
- 学习JDK源码:可进一步优化的代码
- 关于BO新建权限问题
- linux用互斥量进行同步
- as3 eval实现
- android 属性
- UIWebView网页视图
- eclipse 项目中加入tomcat运行环境
- 怎样可以删除多余的桌面右键选项
- codec engine代码阅读七---codecs中的xDM,XDAIS函数解析
- android 联系人 --- 读取usim卡的邮箱
- js常用知识点