⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testosmsgqbroadcast.c

📁 ReWorks里一个有关消息队列的例子。
💻 C
字号:
#include "msgqTest.h"

#define MESSAGE_SIZE (sizeof(long) * 4)  
static long    buffer[ MESSAGE_SIZE / sizeof(long) ];
static long    recBuf1[ MESSAGE_SIZE / sizeof(long) ];
static long    recBuf2[ MESSAGE_SIZE / sizeof(long) ];
static unsigned32 MsgQ_id;
static unsigned32 task_id1,task_id2;
static unsigned32 msgq_size,task_count;

static void entryTsk1(
  void* argument
)
{
    OS_STATUS status;
    printf("in task1:receive msg,because there is no msg in MsgQ,so task1 blocked.\n");
    status = OSMsgQReceive(MsgQ_id,recBuf1,&msgq_size,OS_WAIT,OS_NO_TIMEOUT);
    if(status != OS_OK)
    {
 		printf("OSMsgQReceive error:[%s]\n",serrno(errno));
		return (-1);
 	}
    printf("in task1:receive the buf \"%s\" success,task1 continue run.\n",(char*)recBuf1);
    OSTaskDelete(task_id1);
}

static void entryTsk2(
  void* argument
)
{
    OS_STATUS status;
    printf("in task2:receive msg,because there is no msg in MsgQ,so task2 blocked.\n");
    status = OSMsgQReceive(MsgQ_id,recBuf2,&msgq_size,OS_WAIT,OS_NO_TIMEOUT);
    if(status != OS_OK)
    {
 		printf("OSMsgQReceive error:[%s]\n",serrno(errno));
		return (-1);
 	}
    printf("in task2:receive the buf \"%s\" success,task2 continue run.\n",(char*)recBuf1);
 	
 	OSTaskDelete(task_id2);
}

int testOSMsgQBroadcast()
{
    OS_STATUS status;
    
    
    printf("*** test of OSMsgQBroadcast ***\n");
    printf("create a MsgQ.\n");
    status = OSMsgQCreate("msg",10,20,OS_DEFAULT,&MsgQ_id); 
    if(status != OS_OK)
    {
 		printf("OSMsgQCreate error:[%s]\n",serrno(errno));
		return (-1);
 	}
 	
 	printf("create two tasks.\n");
 	status = OSTaskCreate("tsk1",101,STACK_MINIMUM_SIZE,OS_DEFAULT,OS_DEFAULT,&task_id1); 
	if(status != OS_OK)
    {
 		printf("OSTaskCreate error:[%s]\n",serrno(errno));
		return (-1);
 	}
 	status = OSTaskCreate("tsk2",101,STACK_MINIMUM_SIZE,OS_DEFAULT,OS_DEFAULT,&task_id2); 
	if(status != OS_OK)
    {
 		printf("OSTaskCreate error:[%s]\n",serrno(errno));
		return (-1);
 	}
 	printf("start the two receive tasks.\n");
 	status = OSTaskStart( task_id1, entryTsk1, 0 );  
   	if(status != OS_OK)
  	{
		printf("OSTaskStart error:[%s]\n",serrno(errno));
		return (-1);
	}
	status = OSTaskStart( task_id2, entryTsk2, 0 );  
   	if(status != OS_OK)
  	{
		printf("OSTaskStart error:[%s]\n",serrno(errno));
		return (-1);
	}
 	
 	OSTaskDelay(500);
 	
 	
	strcpy( buffer, "Broadcast" );
 	printf("OSMsgQBroadcast  the buffer \"Broadcast\" to MsgQ.\n");	
	status = OSMsgQBroadcast( MsgQ_id, buffer, MESSAGE_SIZE,&task_count ); 
    if(status != OS_OK)
    {
 		printf("OSMsgQBroadcast error:[%s]\n",serrno(errno));
		return (-1);
 	}	
  	
  	OSTaskDelay(500);
   	OSMsgQDelete(MsgQ_id); 
   	printf("*** OSMsgQBroadcast test OK! ***\n\n");
	return 1;
}   
 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -