| 27 | |
| 2008.7 |
前两天为了在C下编写一个类似Erlang式消息传递的框架,需要定义一个receive宏,使得coroutine处理函数的编写可以类似这样:
int foo()
{
...
receive(msg)
{
// Deal with the message
}
...
receive(msg)
{
// Deal with another message
}
...
}
因为foo()函数以coroutine方式执行,所以消息的及时释放就显得尤为重要了,receive()之后紧接的代码块就是这个消息的生存周期,完成之后需要立即对消息进行释放。这就对receive宏的编写提出了一个特别的要求:包含扫尾工作。