linux下的C语言开发(线程互斥)
来源:互联网 发布:酷云eye麻雀收视数据 编辑:程序博客网 时间:2024/05/08 16:26
对于编写多线程的朋友来说,线程互斥是少不了的。在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 线程互斥
- cocoapods在OS X 10.11的安装
- C#使用Log4Net记录日志(转的)
- C++学习笔记(杂)
- iOS 懒加载模式
- Android BLE开发之Android手机与BLE终端通信
- linux下的C语言开发(线程互斥)
- iOS app上线加急 审核加急
- Difference Between ClassNotFoundException Vs NoClassDefFoundError In Java
- caffe 练习2 用自己的数据集在ImageNet 测试------by 香蕉麦乐迪
- Java基础笔记-继承、抽象类、接口、多态、内部类、异常
- nginx upstream的几种配置方式
- GarfieltBlog-webpy建设
- C/S结构网络开发与B/S结构网络开发认识
- ffmpeg记忆