Linux下线程互斥,只需多加三行代码!
来源:互联网 发布:快搜极速浏览器 知乎 编辑:程序博客网 时间:2024/05/20 02:22
pthread_mutex_tmutex =PTHREAD_MUTEX_INITIALIZER;
intpthread_mutex_lock(pthread_mutex_t *mutex);
intpthread_mutex_unlock(pthread_mutex_t *mutex);
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *func(void *arg){
pthread_mutex_lock(&mutex);
int *a = (int *)arg;
printf("thread%d start\n", *a);
int i;
for(i=0;i<10;i++)
{
printf("thread%d is running\n", *a);
sleep(1);
}
printf("thread%d end\n", *a);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main(int arg, char * args[])
{
printf("process start\n");
pthread_t thr_d1, thr_d2;
int i[2];
i[0] = 1; i[1] =2;
pthread_create(&thr_d1, NULL, func, &i[0]);
pthread_create(&thr_d2, NULL, func, &i[1]);
pthread_join(thr_d1, NULL);
pthread_join(thr_d2, NULL);
printf("process end\n");
return 0;
}
0 0
- Linux下线程互斥,只需多加三行代码!
- linux 下线程的互斥
- Linux下线程同步互斥
- Linux环境下线程的互斥
- Linux下线程互斥与同步
- 【Linux】线程互斥
- linux 线程互斥
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- Linux下线程与进程同步与互斥
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(线程互斥)
- linux下线程的同步和互斥
- Linux下的C语言开发(线程互斥)
- Linux下的线程同步与互斥
- linux c 线程互斥
- linux c 线程互斥
- 阿里云云主机未备案提供网站服务的方法——网站加密80端口映射
- OpenCV 的 GrabCut 可运行代码 及实例
- 坏块处理一
- android笔记1——开发环境的搭建
- 雾山的Robotium学习笔记---环境搭建及入门示例
- Linux下线程互斥,只需多加三行代码!
- ArcGIS中标注的基本词汇
- 黑马程序员_API_泛型&map集合
- Android版QQ实现附源码
- 树欲静而风不止,子欲养而亲不待
- Eclipse编辑java文件报Unhandled event loop exception错误的解决办法
- httpclient 4.3教程第一章 基本概念
- Groovy探索之闭包 三
- java常用算法