linux下的C语言开发(线程互斥)
来源:互联网 发布:网吧电影源码 编辑:程序博客网 时间:2024/05/16 14:01
【原文:http://blog.csdn.net/feixiaoxing/article/details/7243664】
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
对于编写多线程的朋友来说,线程互斥是少不了的。在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!
0 0
- 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 线程互斥
- Effective C++ Item 5 了解 C++ 默默编写并调用哪些函数
- IPTV之Roku开发指南2:下载应用程序
- 结构体内的#define宏定义
- Android获取设备屏幕大小的方法
- LeetCode——Palindrome Partition
- linux下的C语言开发(线程互斥)
- 请教一个 quick Cocos2dx Lua扩展 protobuf的问题
- log4j日志输出性能优化-缓存、异步
- 关于String s = new String("xyz"); 创建几个对象的问题
- 再次写给我们这些浮躁的程序员
- 图像分割之(六)交叉视觉皮质模型(ICM)
- 解决log4j BufferedIO=true 时,buffer有残余日志不能记录的问题
- linux下的C语言开发(网络编程)
- 黑马程序员 Java高新技术-jdk1.5深入反射和泛型