📄 quetest1.c
字号:
/*
* ======== quetest1.c ========
*/
#include <std.h>
#include <log.h>
#include <mem.h>
#include <que.h>
#include <sys.h>
#include <tsk.h>
#include "quetest1cfg.h"
#define NUMMSGS 5 /* 信息的数目*/
typedef struct MsgObj {
QUE_Elem elem; /*结构的第一个域是一个QUE_Elem类型*/
Char val; /*这里将一个字母作为一个消息加入对列,故定义了一个字符型的变量*/
} MsgObj, *Msg;
Void reader(Void); /*读任务函数,用于将queue队列中的消息读出,并放入到freequeue队列中*/
Void writer(Void); /*执行写任务,用于从freequeue队列中取出一个元素,将其赋值后在加入到queue队列中*/
/*
* ======== main ========
*/
Void main()
{
LOG_printf(&trace, "quetest1 example started.\n");
}
/*
* ======== initTask ========
*/
Void initTask()
{
Int i;
MsgObj *msg;
msg = (MsgObj *)MEM_alloc(0, NUMMSGS * sizeof(MsgObj), 0); /*动态分配内存*/
if (msg == MEM_ILLEGAL) {
SYS_abort("Memory allocation failed!\n");
}
/*将所有的消息加入到freequeue队列中,此时freequeue中是没有内容的,它实际上是确定了队列中存放消息的大小*/
for (i = 0; i < NUMMSGS; msg++, i++) {
QUE_put(&freequeue, msg);
}
}
/*
* ======== task ========
*/
Void task()
{
/*
* writer()的调用必须在reader()之前,以确保读任务执行之前对列不是空的
*/
for (;;) { /*循环调用执行写和读任务*/
writer();
reader();
/* 产生一些后台时间*/
TSK_sleep(5);
}
}
/*
* ======== reader ========
*/
Void reader()
{
Msg msg;
Int i;
for (i = 0; i < NUMMSGS; i++) {
/* 执行对任务的时候队列不能为空,所以这里首先测试队列是否为空 */
if (QUE_empty(&queue)) {
SYS_abort("queue error\n");
}
/* 从队列queue中将消息取出 */
msg = QUE_get(&queue);
/* 输出消息的内容 */
LOG_printf(&trace, "read '%c'.", msg->val);
/* 将消息放入到freequeue队列中 */
QUE_put(&freequeue, msg);
}
}
/*
* ======== writer ========
*/
Void writer()
{
Msg msg;
Int i;
for (i = 0; i < NUMMSGS; i++) {
msg = QUE_get(&freequeue); /*从队列freequeue中取出一条消息*/
/* 为该消息赋值 */
msg->val = (i & 0xf) + 'a';
LOG_printf(&trace, "writing '%c' ...", msg->val); /*输出该消息的内容*/
/* 将消息加入到queue队列中*/
QUE_put(&queue, msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -