mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-24 13:13:54 +08:00
add more Chinese comments.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@525 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* 程序清单:动态线程
|
||||
* 程序清单:消息队列例程
|
||||
*
|
||||
* 这个程序会初始化2个动态线程,它们拥有共同的入口函数,但参数不相同
|
||||
* 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
|
||||
* 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
@@ -11,9 +12,12 @@ static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
static rt_thread_t tid3 = RT_NULL;
|
||||
|
||||
/* 消息队列控制块 */
|
||||
static struct rt_messagequeue mq;
|
||||
/* 消息队列中用到的放置消息的内存池 */
|
||||
static char msg_pool[2048];
|
||||
|
||||
/* 线程1入口函数 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
char buf[128];
|
||||
@@ -22,15 +26,18 @@ static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_memset(&buf[0], 0, sizeof(buf));
|
||||
|
||||
/* 从消息队列中接收消息 */
|
||||
if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
|
||||
{
|
||||
rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf);
|
||||
}
|
||||
|
||||
rt_thread_delay(100);
|
||||
/* 延迟10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口函数 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
int i, result;
|
||||
@@ -43,18 +50,22 @@ static void thread2_entry(void* parameter)
|
||||
buf[sizeof(buf) - 2] = '0' + i;
|
||||
|
||||
rt_kprintf("thread2: send message - %s\n", buf);
|
||||
/* 发送消息到消息队列中 */
|
||||
result = rt_mq_send(&mq, &buf[0], sizeof(buf));
|
||||
if ( result == -RT_EFULL);
|
||||
if ( result == -RT_EFULL)
|
||||
{
|
||||
rt_kprintf("message queue full, delay 10s\n");
|
||||
rt_thread_delay(1000);
|
||||
/* 消息队列满, 延迟1s时间 */
|
||||
rt_kprintf("message queue full, delay 1s\n");
|
||||
rt_thread_delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
rt_thread_delay(100);
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程3入口函数 */
|
||||
static void thread3_entry(void* parameter)
|
||||
{
|
||||
char buf[] = "this is an urgent message!";
|
||||
@@ -62,15 +73,23 @@ static void thread3_entry(void* parameter)
|
||||
while (1)
|
||||
{
|
||||
rt_kprintf("thread3: send an urgent message\n");
|
||||
|
||||
/* 发送紧急消息到消息队列中 */
|
||||
rt_mq_urgent(&mq, &buf[0], sizeof(buf));
|
||||
|
||||
rt_thread_delay(250);
|
||||
/* 延时25个OS Tick */
|
||||
rt_thread_delay(25);
|
||||
}
|
||||
}
|
||||
|
||||
int messageq_simple_init()
|
||||
{
|
||||
rt_mq_init(&mq, "mqt", &msg_pool[0], 128 - sizeof(void*), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
|
||||
/* 初始化消息队列 */
|
||||
rt_mq_init(&mq, "mqt",
|
||||
&msg_pool[0], /* 内存池指向msg_pool */
|
||||
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
|
||||
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
|
||||
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
||||
|
||||
/* 创建线程1 */
|
||||
tid1 = rt_thread_create("t1",
|
||||
@@ -116,7 +135,7 @@ static void _tc_cleanup()
|
||||
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid3);
|
||||
|
||||
/* 执行消息队列脱离 */
|
||||
/* 执行消息队列对象脱离 */
|
||||
rt_mq_detach(&mq);
|
||||
|
||||
/* 调度器解锁 */
|
||||
|
||||
Reference in New Issue
Block a user