#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5
#define THREAD_STACK_SIZE 512
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";
static rt_thread_t thread1 = RT_NULL;
static rt_thread_t thread2 = RT_NULL;
static void thread1_entry(void *parameter)
{
char *str;
while(1)
{
rt_kprintf("thread1: try th recv a mail\n");
if(rt_mb_recv(&mb,(rt_uint32_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);
}
static void thread2_entry(void *parameter)
{
rt_uint8_t count;
count=0;
while(count<10)
{
count++;
if(count & 0x1)
{
rt_mb_send(&mb,(rt_uint32_t)&mb_str1);
}
else
{
rt_mb_send(&mb,(rt_uint32_t)&mb_str2);
}
rt_thread_mdelay(200);
}
rt_mb_send(&mb,(rt_uint32_t)&mb_str3);
}
void mail_init(void)
{
rt_err_t result;
result=rt_mb_init(&mb,"mbt",&mb_pool[0],sizeof(mb_pool),RT_IPC_FLAG_FIFO);
thread1=rt_thread_create(
"thread1",
thread1_entry,
RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY,
THREAD_TIMESLICE);
thread2=rt_thread_create(
"thread2",
thread2_entry,
RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY,
THREAD_TIMESLICE);
if(thread1!=RT_NULL) rt_thread_startup(thread1);
if(thread2!=RT_NULL) rt_thread_startup(thread2);
}
int main(void)
{
mail_init();
return RT_EOK;
}
MSH_CMD_EXPORT(mail_init,mail_init);
转载请注明原文地址:https://blackberry.8miu.com/read-1203.html