linux下的C语言开发(线程互斥)
来源:互联网 发布:根据小说改编的网络剧 编辑:程序博客网 时间:2024/04/30 03:42
对于编写多线程的朋友来说,线程互斥是少不了的。在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 线程互斥
- what is policy server
- linux下的C语言开发(多线程编程)
- 第8章 高效开发和使用插件 (三)
- linux下的C语言开发(线程等待)
- VC6.0 中文显示乱码的解决方法
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(网络编程)
- 图片资源中总会出现thumb.db文件
- ZOJ2771题解
- dl-provider.com/MediaCrawler 一个坑爹的网站
- PMON中autoload自动加载操作系统代码分析
- shell 中的find -mtime
- Servlet 工作原理解析
- Hadoop之combiner和partitioner