vxWorks消息队列示例


  
#include "vxWorks.h"    
#include "msgQLib.h"   
    
/* defines */    
#define MAX_MSGS (10)    
#define MAX_MSG_LEN (100)   
    
MSG_Q_ID myMsgQId;   
   
//任务3仅仅创建消息队列
task3(void)   
{   
	/* create message queue */   
    if ((myMsgQId = msgQCreate (MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY)) == NULL)    
        return (ERROR);   
}   
    
//任务2完成接收,并显示
task2 (void)    
{    
    char msgBuf[MAX_MSG_LEN];   
    
    
    /* get message from queue; if necessary wait until msg is available */    
    if (msgQReceive(myMsgQId, msgBuf, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)    
        return (ERROR);   
    
    /* display message */    
    logMsg ("Message from task 1:\n%s\n", msgBuf, 0,0,0,0,0);
}   
    
//任务1里延时5秒,验证【任务2】要等到消息到来时,才会有输出
#define MESSAGE "Greetings from Task 1"    
task1 (void)    
{    
	taskDelay (sysClkRateGet()*5);//延时5秒
	
   if (msgQSend (myMsgQId, MESSAGE, MAX_MSG_LEN, WAIT_FOREVER,    
                  MSG_PRI_NORMAL) == ERROR)    
        return (ERROR);    
   
    /* send a normal priority message, blocking if queue is full  
    if (msgQSend (myMsgQId, MESSAGE, sizeof (MESSAGE), WAIT_FOREVER,   
                  MSG_PRI_NORMAL) == ERROR)   
        return (ERROR);   
     */   
}   
    
void TestMsgQ(void)   
{   
	taskSpawn("t3",100,0,0x20000,(FUNCPTR)task3,0,0,0,0,0,0,0,0,0,0);   
	taskSpawn("t2",100,0,0x20000,(FUNCPTR)task2,0,0,0,0,0,0,0,0,0,0);   
	taskSpawn("t1",100,0,0x20000,(FUNCPTR)task1,0,0,0,0,0,0,0,0,0,0);   
}   
   
   




你可能感兴趣的:(vxWorks消息队列示例)