android消费者代码,Android C++生产者与消费者简易Demo

1. C++生产者与消费者

在了解生产者与消费者之前,我们先来看看 C++ 关于线程的一些基本知识:

线程锁

pthread_mutex_t :用于创建线程锁对象如:pthread_mutex_t mutex;

pthread_mutex_init :用于初始化pthread_mutex_t锁对象如:pthread_mutex_init(&mutex, NULL);

pthread_mutex_destroy :用于销毁pthread_mutex_t锁对象如:pthread_mutex_destroy(&mutex);

线程信号条件

pthread_cond_t :用于创建线程条件对象如:pthread_cond_t cond;

pthread_cond_init :用于初始化pthread_cond_t条件对象如:

pthread_cond_init(&cond, NULL);

pthread_cond_destroy :用于销毁pthread_cond_t条件对象如:

pthread_cond_destroy(&cond);

加锁

pthread_mutex_lock :用于上锁mutex,本线程上锁后的其他变量是不能被别的线程操作的如:pthread_mutex_lock(&mutex);

pthread_mutex_unlock :用于解锁mutex,解锁后的其他变量可以被其他线程操作如:pthread_mutex_unlock(&mutex);

释放信号

pthread_cond_signal :用于发出条件信号如:pthread_cond_signal(&mutex, &cond);

线程等待

pthread_cond_wait :用于线程阻塞等待,直到pthread_cond_signal发出条件信号后

才执行退出线程阻塞执行后面的操作。

1.1 创建生产者和消费者线程的变量

为了模拟队列,我们先定义一个队列,并且预先往队列中添加 10 个数据。

//定义一个队列

std::queue queue;

//预先往队列中添加10个数据

for (int i = 0; i < 10; i++) {

queue.push(i);

}

pthread_mutex_init 创建线程锁

//线程锁

pthread_mutex_t pthread_mutex;

//创建锁

pthread_mutex_init(&pthread_mutex, NULL);

pthread_cond_init 创建线程条件变量

//线程条件对象

pthread_cond_t pthread_cond;

//创建线程条件变量

pthread_cond_init(&pthread_cond, NULL);

pthread_create 创建两个线程

//生产者线程

pthread_t product;

//消费者线程

pthread_t consumer;

//创建对应的线程,以及对应的回调函数

pthread_create(&product, NULL, productCallback, NULL);

pthread_create(&consumer, NULL, consumerCallback, NULL);

1.2 处理生产者回调

因为生产者是需要不断生产产品的,因此加入一个 isProcessing 的 boolean 变量来判断是否要停止生产。

处理生产者主要有以下几步:

加锁

往队列中加入数据

释放信号

解锁

void *productCallback(void *data) {

//在开始线程时,该变量为true

while (isProcessing) {

//加锁

pthread_mutex_lock(&pthread_mutex);

//生产一条数据

queue.push(1);

LOGI("生产者生产一个产品,目前还有%d个产品", queue.size());

//通知消费者去消费

pthread_cond_signal(&pthread_cond);

//解锁

pthread_mutex_unlock(&pthread_mutex);

//每生产一个产品睡眠5秒

sleep(5);

}

//退出线程---暂时不会执行这段代码

pthread_exit(&product);

}

1.3 处理消费者线程回调

消费者需要不断消费队列中的数据,当队列中没有数据时,消费者线程需要等待生产者线程生产数据,如果队列有数据,则直接消费队列的数据,然后释放锁

void *consumerCallback(void *data) {

//消费不断的消费

while (isProcessing) {

//加锁

pthread_mutex_lock(&pthread_mutex);

if (queue.size() > 0) {

queue.pop();

LOGI("消费者消费一个产品,还有%d个产品", queue.size());

} else {

LOGI("当前没产品,等待生产者生产");

pthread_cond_wait(&pthread_cond, &pthread_mutex);

}

//解锁

pthread_mutex_unlock(&pthread_mutex);

//睡眠500ms

usleep(500 * 1000);

}

//退出线程---暂时不会执行这段代码

pthread_exit(&consumer);

}

1.4 停止线程执行

extern "C"

JNIEXPORT void JNICALL

Java_com_example_lib_JniThreadDemo_stopMutex(JNIEnv *env, jobject instance) {

isProcessing = false;

pthread_mutex_destroy(&pthread_mutex);

pthread_cond_destroy(&pthread_cond);

}

1.4 Java 层调用

//生产者与消费者

JniThreadDemo jniThreadDemo = new JniThreadDemo();

public void mutex(View view) {

jniThreadDemo.mutex();

}

//生产者与消费者

public void stopMutex(View view) {

jniThreadDemo.stopMutex();

}

1.5 源码

完整代码:

#include "pthread.h"

#include "queue"

//sleep

#include "unistd.h"

//定义一个队列

std::queue queue;

//生产者线程

pthread_t product;

//消费者线程

pthread_t consumer;

//线程锁

pthread_mutex_t pthread_mutex;

//线程条件对象

pthread_cond_t pthread_cond;

bool isProcessing = true;

//生产者回调处理

void *productCallback(void *data) {

while (isProcessing) {

//死循环生产

//加锁

pthread_mutex_lock(&pthread_mutex);

//生产一条数据

queue.push(1);

LOGI("生产者生产一个产品,目前还有%d个产品", queue.size());

//通知消费者去消费

pthread_cond_signal(&pthread_cond);

//解锁

pthread_mutex_unlock(&pthread_mutex);

//每生产一个产品睡眠5秒

sleep(5);

}

//退出线程---暂时不会执行这段代码

pthread_exit(&product);

}

//消费者回调处理

void *consumerCallback(void *data) {

//消费不断的消费

while (isProcessing) {

//加锁

pthread_mutex_lock(&pthread_mutex);

if (queue.size() > 0) {

queue.pop();

LOGI("消费者消费一个产品,还有%d个产品", queue.size());

} else {

LOGI("当前没产品,等待生产者生产");

pthread_cond_wait(&pthread_cond, &pthread_mutex);

}

//解锁

pthread_mutex_unlock(&pthread_mutex);

//睡眠500ms

usleep(500 * 1000);

}

//退出线程---暂时不会执行这段代码

pthread_exit(&consumer);

}

//开启线程

extern "C"

JNIEXPORT void JNICALL

Java_com_liaowj_jni_thread_JniThreadDemo_mutex(JNIEnv *env, jobject instance) {

if (isProcessing) {

return;

}

isProcessing = true;

//预先往队列中添加10个数据

for (int i = 0; i < 10; i++) {

queue.push(i);

}

//创建锁

pthread_mutex_init(&pthread_mutex, NULL);

//创建条件

pthread_cond_init(&pthread_cond, NULL);

//创建对应的线程

pthread_create(&product, NULL, productCallback, NULL);

pthread_create(&consumer, NULL, consumerCallback, NULL);

}

//停止线程

extern "C"

JNIEXPORT void JNICALL

Java_com_liaowj_jni_thread_JniThreadDemo_stopMutex(JNIEnv *env, jobject instance) {

isProcessing = false;

pthread_mutex_destroy(&pthread_mutex);

pthread_cond_destroy(&pthread_cond);

}

21fa635b719a

生产者与消费者

记录于 2018年11月11日 光棍节~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值