1.代码
# include <rtthread.h>
# include <rtdevice.h>
# include "drv_gpio.h"
# define THREAD_PRIORITY 30
# define THREAD_TIMESLICE 10
# define TASK_STACK_MAX 500
static struct rt_mailbox mb;
static char mb_pool[ 128 ] ;
static char mb_str1[ ] = "I'm a mail!" ;
static char mb_str2[ ] = "this is another mail!" ;
static char mb_str3[ ] = "over" ;
ALIGN ( RT_ALIGN_SIZE)
static char thread41_stack[ TASK_STACK_MAX] ;
static struct rt_thread thread41;
static void thread41_entry ( void * parameter)
{
char * str;
while ( 1 )
{
rt_kprintf ( "thread1: try to recv a mail\n" ) ;
if ( rt_mb_recv ( & mb, ( rt_ubase_t * ) & str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf ( "thread1: get a mail from mailbox, the content:%s\n" , str) ;
if ( str == mb_str3)
break ;
rt_thread_mdelay ( 100 ) ;
}
}
rt_mb_detach ( & mb) ;
}
void MbKeyScanCallBack ( void )
{
static rt_uint8_t count = 0 ;
count ++ ;
if ( count & 0x1 )
{
rt_mb_send ( & mb, ( rt_uint32_t ) & mb_str1) ;
}
else
{
rt_mb_send ( & mb, ( rt_uint32_t ) & mb_str2) ;
}
}
int MbTaskInit ( void )
{
rt_err_t result;
result = rt_mb_init ( & mb,
"mbt" ,
& mb_pool[ 0 ] ,
sizeof ( mb_pool) / 4 ,
RT_IPC_FLAG_FIFO) ;
if ( result != RT_EOK)
{
rt_kprintf ( "init mailbox failed.\n" ) ;
return - 1 ;
}
rt_thread_init ( & thread41,
"thread41" ,
thread41_entry,
RT_NULL,
& thread41_stack[ 0 ] ,
sizeof ( thread41_stack) ,
THREAD_PRIORITY, THREAD_TIMESLICE) ;
rt_thread_startup ( & thread41) ;
return 0 ;
}
2.输出
key press val is 1
t3 take a static semaphore.
thread1: get a mail from mailbox, the content:I'm a mail!
thread1: try to recv a mail
key press val is 1
t3 take a static semaphore.
thread1: get a mail from mailbox, the content:this is another mail!
thread1: try to recv a mail