?? 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 /* 信息的數(shù)目*/
typedef struct MsgObj {
QUE_Elem elem; /*結(jié)構(gòu)的第一個域是一個QUE_Elem類型*/
Char val; /*這里將一個字母作為一個消息加入對列,故定義了一個字符型的變量*/
} MsgObj, *Msg;
Void reader(Void); /*讀任務(wù)函數(shù),用于將queue隊列中的消息讀出,并放入到freequeue隊列中*/
Void writer(Void); /*執(zhí)行寫任務(wù),用于從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); /*動態(tài)分配內(nèi)存*/
if (msg == MEM_ILLEGAL) {
SYS_abort("Memory allocation failed!\n");
}
/*將所有的消息加入到freequeue隊列中,此時freequeue中是沒有內(nèi)容的,它實際上是確定了隊列中存放消息的大小*/
for (i = 0; i < NUMMSGS; msg++, i++) {
QUE_put(&freequeue, msg);
}
}
/*
* ======== task ========
*/
Void task()
{
/*
* writer()的調(diào)用必須在reader()之前,以確保讀任務(wù)執(zhí)行之前對列不是空的
*/
for (;;) { /*循環(huán)調(diào)用執(zhí)行寫和讀任務(wù)*/
writer();
reader();
/* 產(chǎn)生一些后臺時間*/
TSK_sleep(5);
}
}
/*
* ======== reader ========
*/
Void reader()
{
Msg msg;
Int i;
for (i = 0; i < NUMMSGS; i++) {
/* 執(zhí)行對任務(wù)的時候隊列不能為空,所以這里首先測試隊列是否為空 */
if (QUE_empty(&queue)) {
SYS_abort("queue error\n");
}
/* 從隊列queue中將消息取出 */
msg = QUE_get(&queue);
/* 輸出消息的內(nèi)容 */
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); /*輸出該消息的內(nèi)容*/
/* 將消息加入到queue隊列中*/
QUE_put(&queue, msg);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -