互斥锁和条件变量的结合使用
来源:互联网 发布:asp淘宝客源码 编辑:程序博客网 时间:2024/06/06 13:27
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <iostream>
using namespace std;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;//初始构造条件变量
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;//初始构造锁
pthread_t tid1,tid2,tid3;
int x=10;
int y=20;
void*thrd_1(void*arg)
{
pthread_mutex_lock(&qlock);
printf("jiangxin before the pthread_cond_wait\n");
pthread_cond_wait(&qready,&qlock); //个函数在qready条件没满足的时候会卡在这里,并且,会把传入的互斥锁解锁,为什么要解锁的,拟可以想想,
//如果不解锁的话,那外部就没有可以对x,y的修改权,应为其他两个线程想要修改这两个值的话都需要对qclock进行枷锁
printf("jiangxin after the pthread_cond_wait\n");
pthread_mutex_unlock(&qlock);
cout<<"thread 1"<<endl;
}
void*thrd_2(void*arg)
{
pthread_mutex_lock(&qlock);
x=20;
y=10;
cout<<"has change x and y"<<endl;
pthread_mutex_unlock(&qlock);
if(x>y)
{
printf("jiangxin the x is bigger than y\n");
pthread_cond_signal(&qready);
}
cout<<"thread 2"<<endl;
}
void*thrd_3(void*arg)
{
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
cout<<"thread 3"<<endl;
}
int main(int argc,char**argv)
{
int err;
err = pthread_create(&tid1,NULL,thrd_1,NULL);
if(err!=0)
{
cout<<"pthread1 create error"<<endl;
}
err = pthread_create(&tid2,NULL,thrd_2,NULL);
if(err!=0)
{
cout<<"pthread2 create error"<<endl;
}
err = pthread_create(&tid3,NULL,thrd_3,NULL);
if(err!=0)
{
cout<<"pthread3 create error"<<endl;
}
while(1)
{
sleep(1);
}
return 0;
}
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <iostream>
using namespace std;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;//初始构造条件变量
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;//初始构造锁
pthread_t tid1,tid2,tid3;
int x=10;
int y=20;
void*thrd_1(void*arg)
{
pthread_mutex_lock(&qlock);
printf("jiangxin before the pthread_cond_wait\n");
pthread_cond_wait(&qready,&qlock); //个函数在qready条件没满足的时候会卡在这里,并且,会把传入的互斥锁解锁,为什么要解锁的,拟可以想想,
//如果不解锁的话,那外部就没有可以对x,y的修改权,应为其他两个线程想要修改这两个值的话都需要对qclock进行枷锁
printf("jiangxin after the pthread_cond_wait\n");
pthread_mutex_unlock(&qlock);
cout<<"thread 1"<<endl;
}
void*thrd_2(void*arg)
{
pthread_mutex_lock(&qlock);
x=20;
y=10;
cout<<"has change x and y"<<endl;
pthread_mutex_unlock(&qlock);
if(x>y)
{
printf("jiangxin the x is bigger than y\n");
pthread_cond_signal(&qready);
}
cout<<"thread 2"<<endl;
}
void*thrd_3(void*arg)
{
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
cout<<"thread 3"<<endl;
}
int main(int argc,char**argv)
{
int err;
err = pthread_create(&tid1,NULL,thrd_1,NULL);
if(err!=0)
{
cout<<"pthread1 create error"<<endl;
}
err = pthread_create(&tid2,NULL,thrd_2,NULL);
if(err!=0)
{
cout<<"pthread2 create error"<<endl;
}
err = pthread_create(&tid3,NULL,thrd_3,NULL);
if(err!=0)
{
cout<<"pthread3 create error"<<endl;
}
while(1)
{
sleep(1);
}
return 0;
}
阅读全文
0 0
- 互斥锁和条件变量的结合使用
- 互斥锁和条件变量的结合使用
- 条件变量&互斥锁的使用
- 关于条件变量和互斥锁为何配合使用的思考
- linux下互斥锁和条件变量的使用
- 互斥锁和条件变量使用实例
- 条件变量的使用
- 条件变量的使用
- 互斥锁和条件变量
- 互斥锁和条件变量
- 条件变量和互斥锁
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 【SpringMVC】自定义拦截器
- 让C程序更有效的十种方法
- ios-矩形-圆角矩形-椭圆-圆形的绘制
- python cmath的用法
- Java并发
- 互斥锁和条件变量的结合使用
- VUE快速入门心得——render函数
- 多线程共享资源
- uva 10074 Take the Land
- 洛谷1006 传纸条
- liunx 优化及性能调优
- mysql
- 剑指offer面试题目:数组中只出现一次的数字
- Android 的事件分发机制