Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
关于信号量例子的疑问
发布于 2010-01-06 14:15:46 浏览:5769
订阅该版
代码及运行结果如下: /* * File : application.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2006, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at * [http://www.rt-thread.org/license/LICENSE](http://www.rt-thread.org/license/LICENSE) * * Change Logs: * Date Author Notes * 2009-01-05 Bernard the first version */ /** * @addtogroup STM32 */ /*@{*/ #include
#include "led.h" #include
#include "stm32f10x.h " extern void assert_failed(u8* file, u32 line); extern rt_base_t rt_hw_interrupt_disable(void); /* enable interrupt */ extern rt_hw_interrupt_enable(rt_base_t); //当信号量的值等于零时,信号量值加一,并且唤醒等待在该信号量上的线程队列中的首线 //程,由它获取信号量。 //使用信号量的例子 /* 信号量对象*/ struct rt_semaphore sem; /* thread1线程入口*/ void thread1_entry(void* parameter) { while (1) { /* 采用永久等待方式获取信号量*/ rt_kprintf("thread1:taken semaphoreing...... "); if(rt_sem_take(&sem, RT_WAITING_FOREVER) == RT_EOK){ /* 获取到信号量*/ rt_kprintf("thread1:taken semaphore "); rt_thread_delay(10); // rt_sem_release(&sem); } } } /* thread2线程入口*/ void thread2_entry(void* parameter) { //register rt_base_t temp; const char char_c0[] = {"thread2:in the thread2. " }; /* disable interrupt */ while (1) { rt_sem_release(&sem); // temp = rt_hw_interrupt_disable(); rt_kprintf(char_c0); /* enable interrupt */ // rt_hw_interrupt_enable(temp); rt_thread_delay(100); /* 以非等待方式试图获取信号量*/ /* if(rt_sem_take(&sem, RT_WAITING_NO) == RT_EOK){ rt_kprintf("thread2:taken semaphore ok with no waiting "); rt_thread_delay(10); rt_sem_release(&sem); }else{ rt_kprintf("thread2: take semaphore failed with no waiting "); } */ } } int rt_application_init() { rt_thread_t thread; /* 初始化sem对象*/ rt_sem_init(&sem, "semt", 0, 1); /* 创建线程,入口是thread1 entry */ thread = rt_thread_create("t1",thread1_entry, RT_NULL,512, 1, 10); if(thread != RT_NULL){ rt_thread_startup(thread); } /* 创建线程,入口是thread2 entry */ thread = rt_thread_create("t2", thread2_entry, RT_NULL, 512, 2, 10); if(thread != RT_NULL){ rt_thread_startup(thread); } return 0; } /*@}*/ rt_sem_init(&sem, "semt", 0, 0); 运行结果如下: | / - RT - Thread Operating System / | 0.3.0 build Jan 6 2010 2006 - 2009 Copyright by rt-thread team startup a thread:t1 with priority:1 thread resume: t1 startup a thread:t2 with priority:2 thread resume: t2 startup a thread:tidle with priority:31 thread resume: tidle thread1:taken semaphoreing...... thread suspend: t1 thread resume: t1 thread1:taken semaphore thread suspend: t1 thread2:in the thread2. thread suspend: t2 thread1:taken semaphoreing...... thread suspend: t1 thread resume: t1 thread1:taken semaphore thread suspend: t1 thread2:in the thread2. thread suspend: t2 rt_sem_init(&sem, "semt", 0, 1); 运行结果: | / - RT - Thread Operating System / | 0.3.0 build Jan 6 2010 2006 - 2009 Copyright by rt-thread team startup a thread:t1 with priority:1 thread resume: t1 startup a thread:t2 with priority:2 thread resume: t2 startup a thread:tidle with priority:31 thread resume: tidle thread1:taken semaphoreing...... thread suspend: t1 thread resume: (这里出问题了?) thread resume: thread disorder, 4 thread2:in the thread2. thread suspend: t2 thread2:in the thread2. thread suspend: t2 thread2:in the thread2. thread suspend: t2 thread2:in the thread2. thread suspend: t2 thread2:in the thread2. thread suspend: t2
查看更多
8
个回答
默认排序
按发布时间排序
dp_mcu
2010-01-06
这家伙很懒,什么也没写!
rt_sem_init(&sem, "semt", 0, 1); flag为0时正常 flag为1时错误 为什么?
dp_mcu
2010-01-06
这家伙很懒,什么也没写!
flag=1时运行结果: \ | / - RT - Thread Operating System / | \ 0.3.0 build Jan 6 2010 2006 - 2009 Copyright by rt-thread team startup a thread:t1 with priority:1 thread resume: t1 insert thread, the priority: 1 startup a thread:t2 with priority:2 thread resume: t2 insert thread, the priority: 2 startup a thread:tidle with priority:31 thread resume: tidle insert thread, the priority: 31 thread1:taken semaphoreing...... thread suspend: t1 remove thread, the priority: 1 [0]switch to priority#2 thread:t2 thread resume: thread resume: thread disorder, 4 thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2 [0]switch to priority#31 thread:tidle insert thread, the priority: 2 [1]switch to priority#2 thread:t2 switch in interrupt thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2 [0]switch to priority#31 thread:tidle insert thread, the priority: 2 [1]switch to priority#2 thread:t2 switch in interrupt thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2 [0]switch to priority#31 thread:tidle insert thread, the priority: 2 [1]switch to priority#2 thread:t2 switch in interrupt thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2 [0]switch to priority#31 thread:tidle insert thread, the priority: 2 [1]switch to priority#2 thread:t2 switch in interrupt thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2 [0]switch to priority#31 thread:tidle
dp_mcu
2010-01-06
这家伙很懒,什么也没写!
flag=0 \ | / - RT - Thread Operating System / | \ 0.3.0 build Jan 6 2010 2006 - 2009 Copyright by rt-thread team startup a thread:t1 with priority:1 thread resume: t1 insert thread, the priority: 1 startup a thread:t2 with priority:2 thread resume: t2 insert thread, the priority: 2 startup a thread:tidle with priority:31 thread resume: tidle insert thread, the priority: 31 thread1:taken semaphoreing...... thread suspend: t1 remove thread, the priority: 1 [0]switch to priority#2 thread:t2 thread resume: t1 insert thread, the priority: 1 [0]switch to priority#1 thread:t1 thread1:taken semaphore thread suspend: t1 remove thread, the priority: 1 [0]switch to priority#2 thread:t2 thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2 [0]switch to priority#31 thread:tidle insert thread, the priority: 1 [1]switch to priority#1 thread:t1 switch in interrupt thread1:taken semaphoreing...... thread suspend: t1 remove thread, the priority: 1 [0]switch to priority#31 thread:tidle insert thread, the priority: 2 [1]switch to priority#2 thread:t2 switch in interrupt thread resume: t1 insert thread, the priority: 1 [0]switch to priority#1 thread:t1 thread1:taken semaphore thread suspend: t1 remove thread, the priority: 1 [0]switch to priority#2 thread:t2 thread2:in the thread2. thread suspend: t2 remove thread, the priority: 2
bernard
2010-01-06
这家伙很懒,什么也没写!
今天比较忙, thread resume: thread resume: thread disorder, 4 这个是在哪里打印出来的?t1的优先级是1,t2的优先级是2。所以FLAG=1时,应该t1先拿到semaphore。
dp_mcu
2010-01-06
这家伙很懒,什么也没写!
一开始就会打印: 采用优先级方式时,任务1申请信号,没有得到(它的优先级高),所以会挂起, 打印如下:thread1:taken semaphoreing...... thread suspend: t1 接着切换到任务2,任务2发送信号,于是就唤醒了任务1,接着打印: thread resume: //(这里出问题了,冒号后没有字符) thread resume: thread disorder, 4 thread2:in the thread2. thread suspend: t2 thread2:in the thread2. thread suspend: t2 当flag=0时,采用fifo方式时,任务1申请信号,没有得到(它的优先级高),所以会挂起, 打印如下:thread1:taken semaphoreing...... thread suspend: t1 接着切换到任务2,任务2发送信号,于是就唤醒了任务1,接着打印: thread resume: t1 thread1:taken semaphore thread suspend: t1 thread2:in the thread2. thread suspend: t2 thread1:taken semaphoreing...... thread suspend: t1 thread resume: t1 thread1:taken semaphore thread suspend: t1 所以按照上述比较,我认为是t1的挂起操作没有成功,也即 rt_inline rt_err_t rt_ipc_object_suspend(struct rt_ipc_object *ipc, struct rt_thread *thread) { /* suspend thread */ rt_thread_suspend(thread); ipc->suspend_thread_count ++; switch (ipc->parent.flag) { case RT_IPC_FLAG_FIFO: rt_list_insert_before(&(ipc->suspend_thread), &(thread->tlist)); break; case RT_IPC_FLAG_PRIO: { struct rt_list_node* n; struct rt_thread* sthread; /* find a suitable position */ for (n = ipc->suspend_thread.next; n != &(ipc->suspend_thread); n = n->next) { sthread = rt_list_entry(n, struct rt_thread, tlist); /* find out */ if (thread->current_priority < sthread->current_priority) { /* insert this thread before the sthread */ rt_list_insert_before(&(sthread->tlist), &(thread->tlist)); break; } } } break; } return RT_EOK; } 函数,现在我还看不太明白。
bernard
2010-01-06
这家伙很懒,什么也没写!
估计你用的是0.3.0 rc1以前的版本,这个问题softwind说过,并修正了,0.3.0 rc1的代码如下: ``` rt_inline rt_err_t rt_ipc_object_suspend(struct rt_ipc_object *ipc, struct rt_thread *thread) { /* suspend thread */ rt_thread_suspend(thread); ipc->suspend_thread_count ++; switch (ipc->parent.flag) { case RT_IPC_FLAG_FIFO: rt_list_insert_before(&(ipc->suspend_thread), &(thread->tlist)); break; case RT_IPC_FLAG_PRIO: { struct rt_list_node* n; struct rt_thread* sthread; /* find a suitable position */ for (n = ipc->suspend_thread.next; n != &(ipc->suspend_thread); n = n->next) { sthread = rt_list_entry(n, struct rt_thread, tlist); /* find out */ if (thread->current_priority < sthread->current_priority) { /* insert this thread before the sthread */ rt_list_insert_before(&(sthread->tlist), &(thread->tlist)); break; } } /* not found a suitable position, append to the end of suspend_thread list */ if (n == &(ipc->suspend_thread)) rt_list_insert_before(&(ipc->suspend_thread), &(thread->tlist)); } break; } return RT_EOK; } ```
dp_mcu
2010-01-07
这家伙很懒,什么也没写!
确实如此,现在正常了。 /* not found a suitable position, append to the end of suspend_thread list */ if (n == &(ipc->suspend_thread)) rt_list_insert_before(&(ipc->suspend_thread), &(thread->tlist)); 这句关键。
撰写答案
登录
注册新账号
关注者
0
被浏览
5.8k
关于作者
dp_mcu
这家伙很懒,什么也没写!
提问
1
回答
6
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
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 2024开发者大会议程正式发布!
2
【24嵌入式设计大赛】基于RT-Thread星火一号的智慧家居系统
3
RT-Thread EtherKit开源以太网硬件正式发布
4
如何在master上的BSP中添加配置yml文件
5
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
MicroPython
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
19
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
6
个答案
2
次被采纳
用户名由3_15位
13
个答案
1
次被采纳
本月文章贡献
程序员阿伟
9
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
5
次点赞
RTT_逍遥
1
篇文章
4
次点赞
ThinkCode
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部