一个简单的双缓冲通讯模型应该包括有3个线程,分别是:
1. receiving thread 负责来自host端的数据,并且将解析到得正确的命令加入到接收队列 Input queue中。
2. processing thread 负责循环的取接收队列中的命令,并且处理后,将结果放到发送队列 Output queue中。
3. transmitting Thread 负责循环的发送发送队列 Output queue 中的数据。
线程之间的队列的同步采取信号量的方式,关于信号量的具体内容,可以参考相应的资料。
以linux为例: 有sem_init 、 sem_post、sem_wait 、sem_destroy ,分别代表创建一个信号量、信号量+1、信号量消费-1、销毁一个信号量。
简略的伪代码如下
receiving thread :
while(1)
{
recv command;
add to input queue;
sem_post sem of inputqueue ;
}
processing thread :
while(sem_wait sem of inputqueue)
{
process cmd;
add out to the output queue;
sem_post sem of outputqueue ;
}
transmitting Thread :
while(sem_wait sem of outputqueue )
{
send entity in the output queue;
}