Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
Kernel
15天rt-thread入门学习 - 第十二天(生产者-消费者问题)
发布于 2018-05-28 13:14:05 浏览:1882
订阅该版
* 本帖最后由 coolbor 于 2018-5-28 13:27 编辑 * 主要内容: 1、什么是生产者-消费者问题 生产者 ---- 产 品 ---- 消费者 同步与互斥的问题 2、生活中的生产者-消费者问题 ------- | | 工 人 |仓库| 销售员 ------- 3、例程源码解析:使用RT-Thread任务间同步的相关机制给出一个生产者-消费者问题解决模型 ------------------------------------------------------------------ 定义: /* 定义最大5个元素能够被产生 */ #define MAXSEM 5 /* 用于放置生产的整数数组 */ rt_uint32_t array[MAXSEM]; /* 指向生产者、消费者在array数组中的读写位置 */ static rt_uint32_t set, get; /* 指向线程控制块的指针 */ static rt_thread_t producer_tid = RT_NULL; static rt_thread_t consumer_tid = RT_NULL; struct rt_semaphore sem_lock;//解决互斥问题 struct rt_semaphore sem_empty, sem_full;//解决同步问题 ------------------------------------------------------------------ 初始化: rt_sem_init(&sem_lock, "lock", 1, RT_IPC_FLAG_FIFO); rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO); rt_sem_init(&sem_full, "full", 0, RT_IPC_FLAG_FIFO); ------------------------------------------------------------------ 创建任务: 生产者 producer_tid = rt_thread_create("producer", producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); 消费者 consumer_tid = rt_thread_create("consumer", consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); ------------------------------------------------------------------ 生产者行为: /* 获取一个空位 */ rt_sem_take(&sem_empty, RT_WAITING_FOREVER); /* 修改array内容,上锁 */ rt_sem_take(&sem_lock, RT_WAITING_FOREVER); array[set % MAXSEM] = cnt + 1; rt_kprintf("the producer generates a number: %d
", array[set % MAXSEM]); set++; rt_sem_release(&sem_lock); /* 发布一个满位 */ rt_sem_release(&sem_full); ------------------------------------------------------------------ 消费者行为: /* 获取一个满位 */ rt_sem_take(&sem_full, RT_WAITING_FOREVER); /* 临界区,上锁进行操作 */ rt_sem_take(&sem_lock, RT_WAITING_FOREVER); sum += array[get % MAXSEM]; rt_kprintf("the consumer[%d] get a number: %d
", (get % MAXSEM), array[get % MAXSEM]); get++; rt_sem_release(&sem_lock); /* 释放一个空位 */ rt_sem_release(&sem_empty); /* 生产者生产到10个数目,停止,消费者线程相应停止 */ if (get == 10) break; ================================================================= 作业: 1、使用互斥量替换sem_lock,查看程序运行结果。 ------------------------------------------------------------------- 修改后的程序: ```#include
#define THREAD_PRIORITY 6 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 5 /* 定义最大5个元素能够被产生 */ #define MAXSEM 5 /* 用于放置生产的整数数组 */ rt_uint32_t array[MAXSEM]; /* 指向生产者、消费者在array数组中的读写位置 */ static rt_uint32_t set, get; /* 指向线程控制块的指针 */ static rt_thread_t producer_tid = RT_NULL; static rt_thread_t consumer_tid = RT_NULL; //struct rt_semaphore sem_lock; struct rt_mutex mutex_lock; struct rt_semaphore sem_empty, sem_full; /* 生成者线程入口 */ void producer_thread_entry(void *parameter) { int cnt = 0; /* 运行10次 */ while (cnt < 10) { /* 获取一个空位 */ rt_sem_take(&sem_empty, RT_WAITING_FOREVER); /* 修改array内容,上锁 */ //rt_sem_take(&sem_lock, RT_WAITING_FOREVER); rt_mutex_take(&mutex_lock,RT_WAITING_FOREVER); array[set % MAXSEM] = cnt + 1; rt_kprintf("the producer generates a number: %d
", array[set % MAXSEM]); set++; //rt_sem_release(&sem_lock); rt_mutex_release(&mutex_lock); /* 发布一个满位 */ rt_sem_release(&sem_full); cnt++; /* 暂停一段时间 */ rt_thread_delay(50); } rt_kprintf("the producer exit!
"); } /* 消费者线程入口 */ void consumer_thread_entry(void *parameter) { rt_uint32_t no; rt_uint32_t sum; /* 第n个线程,由入口参数传进来 */ no = (rt_uint32_t)parameter; sum = 0; while (1) { /* 获取一个满位 */ rt_sem_take(&sem_full, RT_WAITING_FOREVER); /* 临界区,上锁进行操作 */ //rt_sem_take(&sem_lock, RT_WAITING_FOREVER); rt_mutex_take(&mutex_lock,RT_WAITING_FOREVER); sum += array[get % MAXSEM]; rt_kprintf("the consumer[%d] get a number: %d
", (get % MAXSEM), array[get % MAXSEM]); get++; //rt_sem_release(&sem_lock); rt_mutex_release(&mutex_lock); /* 释放一个空位 */ rt_sem_release(&sem_empty); /* 生产者生产到10个数目,停止,消费者线程相应停止 */ if (get == 10) break; /* 暂停一小会时间 */ rt_thread_delay(10); } rt_kprintf("the consumer[%d] sum is %d", no, sum); rt_kprintf("the consumer[%d] exit!
"); } int mutex_producer_consumer_init() { /* 初始化3个信号量 */ //rt_sem_init(&sem_lock, "lock", 1, RT_IPC_FLAG_FIFO); rt_mutex_init(&mutex_lock, "smutex", RT_IPC_FLAG_FIFO); rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO); rt_sem_init(&sem_full, "full", 0, RT_IPC_FLAG_FIFO); /* 创建线程1 */ producer_tid = rt_thread_create("producer", producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); if (producer_tid != RT_NULL) rt_thread_startup(producer_tid); /* 创建线程2 */ consumer_tid = rt_thread_create("consumer", consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); if (consumer_tid != RT_NULL) rt_thread_startup(consumer_tid); return 0; } /* 如果设置了RT_SAMPLES_AUTORUN,则加入到初始化线程中自动运行 */ #if defined (RT_SAMPLES_AUTORUN) && defined(RT_USING_COMPONENTS_INIT) INIT_APP_EXPORT(mutex_producer_consumer_init); #endif /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(mutex_producer_consumer_init, producer_consumer sample);``` ------------------------------------------------------------------- 运行结果: ``` \ | / - RT - Thread Operating System / | \ 3.0.4 build May 21 2018 2006 - 2018 Copyright by rt-thread team msh >mutex_pr mutex_producer_consumer_init msh >mutex_producer_consumer_init the producer generates a number: 1 the consumer[0] get a number: 1 msh >the producer generates a number: 2 the consumer[1] get a number: 2 the producer generates a number: 3 the consumer[2] get a number: 3 the producer generates a number: 4 the consumer[3] get a number: 4 the producer generates a number: 5 the consumer[4] get a number: 5 the producer generates a number: 6 the consumer[0] get a number: 6 the producer generates a number: 7 the consumer[1] get a number: 7 the producer generates a number: 8 the consumer[2] get a number: 8 the producer generates a number: 9 the consumer[3] get a number: 9 the producer generates a number: 10 the consumer[4] get a number: 10 the consumer[0] sum is 55 the consumer[32] exit! the producer exit!```
查看更多
0
个回答
默认排序
按发布时间排序
暂无答案,快来添加答案吧
撰写答案
登录
注册新账号
关注者
0
被浏览
1.9k
关于作者
coolbor
这家伙很懒,什么也没写!
提问
16
回答
3
被采纳
0
关注TA
发私信
相关问题
1
请教cpu使用率分析
2
选择FreeRTOS, 还是RT-Thread。
3
thread heap stack overflow ?
4
rtt消息队列delay问题
5
释放被删除线程的内存地方在哪里啊
6
请教:各线程结束后,释放其中的内存的连续性问题
7
STM32F103中断关于信号量、邮箱问题
8
RTT中的线程栈大小如何控制
9
关于线程由执行态变为挂起态的代码实现,,,
10
rt_malloc(rt_size_t size)内存分配函数最小分配尺寸问题
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-Thread中的time溢出问题,时间戳溢出,解决方法
2
ART-PI使用env驱动ETH网卡,pc和板子可以ping通
3
SystemView线程名字不显示
4
只用网页也能跑RT-Smart 无门槛腾讯Cloud studio + smart-build快速构建
5
免费申请 | FRDM-MCXA156评测活动发布!
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
8
个答案
2
次被采纳
三世执戟
7
个答案
1
次被采纳
KunYi
6
个答案
1
次被采纳
winfeng
2
个答案
1
次被采纳
chenyaxing
2
个答案
1
次被采纳
本月文章贡献
catcatbing
2
篇文章
5
次点赞
swet123
1
篇文章
3
次点赞
YZRD
1
篇文章
2
次点赞
Days
1
篇文章
2
次点赞
阳光的掌控者
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部