pthread_cond_wait和pthread_cond_signal以及互斥变量的使用情况
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFER_SIZE 21 //初始化存储空间的大小
#define END_FLAG (-1)//用于退出循环
struct Products
{
int buffer[BUFFER_SIZE];//存储空间
pthread_mutex_t locker; //声明互斥变量
pthread_cond_t notEmpty;//声明条件变量
pthread_cond_t notFull;
int posReadFrom;
int posWriteTo;
};
int flag1=,flag2=,num1=,num2=;//变量一定要记得初始化,很重要,前两个为了标记,后两个为了计数
struct Products products; //定义结构体变量,下面要用到的
int BufferIsFull(struct Products* products)//如果满,则返回1,否则返回0;
{
if ((products->posWriteTo + )%BUFFER_SIZE==products->posReadFrom)
{
return ();
}
return ();
} int BufferIsEmpty(struct Products* products)//如果空,则返回1,否则返回0;
{
if ((products->posWriteTo)==(products->posReadFrom))
{
return ();
} return ();
} /*制造产品*/
void Produce(struct Products* products, int item)
{
/*原子操作*/
pthread_mutex_lock(&products->locker);
/*无空间可写入*/
while (BufferIsFull(products))
{flag1=;//标记变量
pthread_cond_wait(&products->notFull, &products->locker);
}
/*写入数据*/
products->buffer[products->posWriteTo] = item;
products->posWriteTo++;
pthread_mutex_unlock(&products->locker);//在解锁之后发射信号
/*发信*/
if(flag2==)
{flag2=;//这行代码至关重要,一开始我就是忘了写,然后会不断输出这句
pthread_cond_signal(&products->notEmpty);
printf("Produce调用了唤醒函数%d次\n",++num1);//检查代码的执行情况是否正确,因为加入这行代码,更容易分析
} } int Consume(struct Products* products)
{
int item; pthread_mutex_lock(&products->locker);
/*为空时持续等待,无数据可读*/
while (BufferIsEmpty(products))
{flag2=;
pthread_cond_wait(&products->notEmpty, &products->locker);
}
/*提取数据*/
item = products->buffer[products->posReadFrom];
products->posReadFrom++;
pthread_mutex_unlock(&products->locker);
if(flag1==)
{flag1=;pthread_cond_signal(&products->notFull);
printf("Consume调用了唤醒函数%d次\n",++num2);} return item;
}
void* ProducerThread(void* data)
{
int i;
for (i = ; i <; ++i)
{
printf("producer: %d\n", i);
Produce(&products, i);
}
Produce(&products, END_FLAG);
return NULL;
} void* ConsumerThread(void* data)
{
int item; while ()
{
item = Consume(&products);
if (END_FLAG == item)
break;
printf("consumer: %d\n", item);
}
return (NULL);
}
int main(int argc, char* argv[])
{
pthread_t producer;
pthread_t consumer;
int result;
pthread_create(&producer, NULL, &ProducerThread, NULL);
pthread_create(&consumer, NULL, &ConsumerThread, NULL);
pthread_join(producer, (void *)&result);
pthread_join(consumer, (void *)&result);
printf("products.posReadFrom=%d\n",products.posReadFrom);//测试循环次数
printf("products.posWriteTo=%d",products.posWriteTo);
exit(EXIT_SUCCESS);
}
//pthread_cond_signal此处我用了if进行判断,因为如果不进行判断,
//则每次都会执行这句,信号发出去了,但是没人接收!(这个思想很重要的)
/*为什么这个结构体变量没有初始化,反而它的两个数据成员,一开始都为0,
真的让我不知道为啥,最后我终于明白了原来因为这个程序中把struct Products结构体变量,
products声明为全局变量所以系统自动初始化了!这方面的只是好久不看都忘了!*/
pthread_cond_wait和pthread_cond_signal以及互斥变量的使用情况的更多相关文章
- 关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题
关于一个消费者模式,,,引起的问题.. 我在io线程里不断的把一个函数调用放到队列里 然后ruby线程就不断的从这个队列里取出函数之争并运行. 典型的 消费者模式. 我曾经以为是这样... 这是wor ...
- 条件变量pthread_cond_wait()和pthread_cond_signal()详解
条件变量 条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立" ...
- 深入理解pthread_cond_wait、pthread_cond_signal
===============================man pthread_cond_wait的解释========================== LINUX环境下多线程编程肯定会遇到 ...
- 面试题:如何在不使用临时变量temp的情况下交换两个整数的值?
利用一个小技巧,一个整数a在异或另一个整数b两次以后所得的值还是整数a. 具体的过程我们可以自己找两个整数以二进制的形式自己在纸上画一下他们的异或过程.(异或的运算符号为"^") ...
- 继承时,当父子类都具有相同的成员变量,默认情况下是直接调用子类的成员变量,当要调用父类的成员变量则需要使用super关键之
package day02; public class Person { String name="fl"; }class Car{ }class Student extends ...
- 【mybatis】mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 或者 feign被调用方使用的mybatis总报空指针异常java.lang.NullPointerException,而变量都没有问题的情况
mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 需要检查的步骤: ...
- 线程相关函数(6)-pthread_cond_wait(),pthread_cond_signal(), 条件变量
pthread_cond_tpthread_cond_initpthread_cond_destroypthread_cond_waitpthread_cond_timedwaitpthread_co ...
- linux多线程-互斥&条件变量与同步
多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...
- node源码详解(七) —— 文件异步io、线程池【互斥锁、条件变量、管道、事件对象】
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource7 本博客同步在https://cnodejs.o ...
随机推荐
- C++中的成员对象
原文链接: http://blog.csdn.net/rhzwan123/article/details/2105205 [概念] 成员对象:当一个类的成员是另一个类的对象时,这个对象就叫成员对象.概 ...
- MySQL 5.7.19 CentOS 7 安装
Linux的版本有很多,因此下载mysql时,需要注意下载对应Linux版本的MySql数据库文件.以下方法也适合centOS 7 的mysql 5.7.* 版本的安装.安装方法我整理为16步. 1: ...
- WinForm DataGridView新增加行
1.不显示最下面的新行 通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * ).如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 Allow ...
- Android基本功:异步任务(AsyncTask)
一.解决新线程无法更新UI组建问题的方案 为了解决新线程不能更新UI组建的问题,Andorid提供了如下几种解决方案: 使用Handler实现线程之间的通信. Activity.runOnUiThre ...
- C# delegate 委托
http://www.runoob.com/csharp/csharp-delegate.html
- ubuntu 14.04安装mysql数据库
1. apt-get install mysql-server mysql-client 输入root的密码: 确认root的密码: 2. 连接测试是否成功:mysql –hlocalhost –ur ...
- 怎么删除Elasticsearch里的index内容
DELETE testindex 请求方式为 DELETE, 跟库名
- Oozie4.2 安装部署、以及example测试
编译: 使用的环境是:Hadoop2.6.0.Spark1.4.0.Hive0.13.1.Sqoop1.4.4 编译Oozie的命令:./mkdistro.sh -Phadoop-2 -Dhadoop ...
- Vivado与SDK的联合调试方法-使用ILA
首先介绍一下我的硬件平台:使用的开发板为米联客出的MIZ702,这个开发板与ZedBoard是兼容的. Vivado硬件调试有几种手段:ILA(集成逻辑分析器Integrated Logic Anal ...
- Pycrypto与RSA密码技术笔记
密码与通信 密码技术是一门历史悠久的技术.信息传播离不开加密与解密.密码技术的用途主要源于两个方面,加密/解密和签名/验签 在信息传播中,通常有发送者,接受者和窃听者三个角色.假设发送者Master想 ...