c - 非嵌入式系统的 RTOS 邮箱示例

标签 c multithreading rtos

我想用 VS2013 在 Windows 8 中编写一个简单的 C 控制台应用程序。

对于线程间通信,我必须使用这样的邮箱实现:

#include <stdio.h> 
#include <Rtk32.h> 

RTKMailbox Box; 

void RTKAPI TaskA(void * P) 
{ 
   int i; 

   printf("Task A: waiting at mailbox\n"); 
   RTKGet(Box, &i); 
   printf("Task A: have received number %i\n", i); 
} 

void main(void) 
{ 
   int i; 

   printf("\n"); 
   RTKernelInit(3); 
   Box = RTKCreateMailbox(sizeof(int), 1, "Test Box"); 
   printf("Main  : creating task A\n"); 
   RTKCreateThread(TaskA, 4, 0, 0, NULL, "Task A"); 
   printf("Main  : please enter a number: "); 
   fflush(stdin); 
   scanf("%i", &i); 
   RTKPut(Box, &i); 
   printf("Main  : done.\n"); 
}

Mailbox

是否有用于非嵌入式系统的库或者什么是最好的方法。

最佳答案

我不确定一个库,但我制作了一个基于 Windows 的多线程文件复制控制台示例程序,其中一个线程读取,另一个线程写入,使用互斥体和信号量之上的 fifo 链表消息传递系统。消息传递函数是 GetNode() 和 PutNode(),并与列表节点一起使用。可以添加另一层函数来使用 ReceiveMessage() 和 SendMessage() 实现类似邮箱的复制接口(interface)。链表可以用循环数组代替,但链表允许可变数量的消息。我已经为多线程嵌入式系统和旧的多线程小型计算机内核使用了类似的链接列表消息传递接口(interface)。 mtcopy.zip

关于c - 非嵌入式系统的 RTOS 邮箱示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30399172/

相关文章:

c - 试图使连续的 FIFO 数据流

c - for 循环在 C 中不能正确递增

linux - 非 RTOS 上的抢占式任务

Java在循环中实例化一个类

java - System.out.println 的多线程输出是否交错

c - 使用外部函数在任务中获取互斥量

process - 'Yield'在Contiki rtos中意味着什么

适用于 OS X 的带有 openmp 的 Python C 扩展

c - 使用 stdin 读取单列数据文件并使用 C 中的函数值返回值

c++ - 此代码中是否存在数据竞争?