linux下的C语言开发(线程互斥)
来源:互联网 发布:苹果手机怎么清楚数据 编辑:程序博客网 时间:2024/05/17 07:27
对于编写多线程的朋友来说,线程互斥是少不了的。在linux下面,编写多线程常用的工具其实是pthread_mutex_t。本质上来说,它和Windows下面的mutex其实是一样的,差别几乎是没有。希望对线程互斥进行详细了解的朋友可以看这里。
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- static int value = 0;
- pthread_mutex_t mutex;
- void func(void* args)
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- sleep(1);
- value ++;
- printf("value = %d!\n", value);
- pthread_mutex_unlock(&mutex);
- }
- }
- int main()
- {
- pthread_t pid1, pid2;
- pthread_mutex_init(&mutex, NULL);
- if(pthread_create(&pid1, NULL, func, NULL))
- {
- return -1;
- }
- if(pthread_create(&pid2, NULL, func, NULL))
- {
- return -1;
- }
- while(1)
- sleep(0);
- return 0;
- }
- [test@localhost thread]$ ./mutex
- value = 1!
- value = 2!
- value = 3!
- value = 4!
- value = 5!
- value = 6!
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- Linux下的C语言开发(线程互斥)
- Linux下C语言多线程(第二篇)线程互斥pthread_mutex_t()
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux 下线程的互斥
- Linux环境下线程的互斥
- linux下的C语言开发(线程等待)
- linux c 线程互斥
- linux c 线程互斥
- linux下的C语言开发(多线程编程)
- socket编程(二)---- 使用套接字连接多个客户端
- sqlit大量数据快速查询(1)
- TCPDUMP命令详解
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程互斥)
- CSS3实现的超酷页面过渡效果
- linux下的C语言开发(网络编程)
- Java通过BufferedWriter追加内容到文件末尾的几种常用方法
- Python使用ElementTree解析XML
- Struts1和Struts2的区别和对比
- js判断Checkbox的全选,反选,多选与只能选一个
- BI
- 敏捷个人俱乐部线下活动2012.02生活完整视频