This example demonstrates how to pass data between threads using message passing.
extern "C" {
void __cxa_pure_virtual(void) {}
void DebugPrint(const char* szString_);
}
namespace
{
#define APP1_STACK_SIZE (PORT_KERNEL_DEFAULT_STACK_SIZE)
K_WORD awApp1Stack[APP1_STACK_SIZE];
void App1Main(void* unused_);
#define APP2_STACK_SIZE (PORT_KERNEL_DEFAULT_STACK_SIZE)
K_WORD awApp2Stack[APP2_STACK_SIZE];
void App2Main(void* unused_);
void IdleMain(void* )
{
while (1) {}
}
#define MESSAGE_POOL_SIZE (3)
Message s_clMessages[MESSAGE_POOL_SIZE];
void App1Main(void* unused_)
{
auto u16Data = 0;
while (1) {
auto* pclMsg = s_clMessagePool.
Pop();
u16Data++;
pclMsg->SetData(&u16Data);
}
}
void App2Main(void* unused_)
{
while (1) {
s_clMessagePool.
Push(pclMsg);
}
}
}
int main(void)
{
clIdleThread.Init(awIdleStack, sizeof(awIdleStack), 0, IdleMain, 0);
clIdleThread.Start();
clApp1Thread.Init(awApp1Stack, sizeof(awApp1Stack), 1, App1Main, 0);
clApp2Thread.Init(awApp2Stack, sizeof(awApp2Stack), 1, App2Main, 0);
clApp1Thread.Start();
clApp2Thread.Start();
clMsgQ.Init();
s_clMessagePool.Init();
for (int i = 0; i < MESSAGE_POOL_SIZE; i++) {
s_clMessages[i].Init();
s_clMessagePool.Push(&s_clMessages[i]);
}
return 0;
}