Thread 中讨论的其他器件:SYSBIOS
工具/软件:TI-RTOS
您好,
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#define NUMMSGS 3 /*消息数*/
#define TIMEOUT 10.
typedef 结构 MsgObj{
int id;/* writer task id */
char val;/*消息值*/
} MsgObj,*Msg;
空读卡器(空);
空写入器(int id_arg);
mailbox_handle mbox;
/*
*==== main ====
*
void main()
{
Task_Params taskParams;
Task_handle myTsk0、myTski;
Mailbox_Params mboxParams;
UINT I;
ERROR_Block EB;
ERROR_INIT (&EB);
/*create 1 reader_task with priority 2*/
Task_Params_init (&taskParams);
// taskParams.STACKSIZE = 4*1024;
taskParams.priority = 2;
myTsk0=Task_create ((Task_Functr)读取器、&taskParams、&EB);
if (myTsk0=NULL)
system_abort ("读取器创建失败");
Mailbox_Params_init (&mboxParams);
mbox = Mailbox_create (sizeof (MsgObj)、2、和 mboxParams、&EB);
if (mbox=NULL)
system_abort ("创建邮箱失败");
/*创建优先级为2*/的3个任务
/*重复使用 taskParams */
taskParams.priority=2;
for (i=0;i<3;i++)
{
taskParams.arg0=i;
myTski=Task_create ((Task_FuncPtr) writer、taskParams、&EB);
if (myTski=NULL)
system_abort ("写入器创建失败");
}
/*启动 SYS/BIOS*/
system_flush();
BIOS_start();
}
/*
*==== 读取器====
*
空读取器(空)
{
MsgObj msg;
while (1)
{
/*等待 writer()发布邮箱*/
if (Mailbox_pend (mbox、&msg、BIOS_wait_forever)=0)//BIOS_wait_forever
{
System_printf ("MBX_PEND 超时()\n"MB 超时);
system_flush();
中断;
}
/*打印值*/
system_printf ("从(%d)读取'%c'。\n"、msg.val、msg.id);
system_flush();
}
System_printf ("读取器完成。\n"\});
system_flush();
}
/*
*==== Writer ====
*
空写入器(int id_arg)
{
MsgObj msg;
int i;
int id = id_arg;// 0;//ArgToInt (id_arg);
对于(i=0;i < NUMMSGS;i++)
{
/*填入值*/
MSG.id = id;
MSG.val = I % NUMMSGS +(Int)('a');
/*排队消息*/
Mailbox_post (mbox、&msg、BIOS_wait_forever);
system_printf ("(%d)写入'%c'...\n"、id、(int) msg.val);
system_flush();
}
System_printf ("写入器(%d)完成。\n"、id);
system_flush();
Task_yield ();
}
控制台中的调试结果如下:
[C674x_0](0)正在写入'A'...
(0)写入'b'...
从(0)读取"A"。
从(0)中读取"b"。
(0)写入'c'...
写入器(0)完成。
(1)写入'A'。
从(0)中读取'c'。
从(1)中读取"A"。
(2)写入'A'。
(1)写入'b'。
从(2)中读取"A"。
从(1)中读取"b"。
(2)写入'b'。
(1)写入'c'。
写入器(1)完成。
从(2)中读取"b"。
从(1)中读取"c"。
(2)写入'c'。
写入器(2)完成。
从(2)中读取"c"。
但我认为结果应如下:
[C674x_0](0)正在写入'A'...
(0)写入'b'...
从(0)读取"A"。
从(0)中读取"b"。
(0)写入'c'...
写入器(0)完成。
(1)写入'A'。
从(0)中读取'c'。
从(1)中读取"A"。
(1)写入'b'。
(1)写入'c'。
写入器(1)完成。
从(1)中读取"b"。
从(1)中读取"c"。
(2)写入'A'。
(2)写入'b'。
从(2)中读取"A"。
从(2)中读取"b"。
(2)写入'c'。
写入器(2)完成。
从(2)中读取"c"。
您能解释一下原因吗? 非常感谢!