标题: 浅谈 C# 中的代码协同 (Coroutine) 执行支持 [打印本页] 作者: 蓝信子 时间: 2008-12-24 11:59 标题: 浅谈 C# 中的代码协同 (Coroutine) 执行支持 几个月前我曾大致分析过 C# 2.0 中 iterator block 机制的实现原理,《C# 2.0 中Iterators的改进与实现原理浅析》,文中简要介绍了 C# 2.0 是如何在不修改 CLR 的前提下由编译器,通过有限状态机来实现 iterator block 中 yield 关键字。
实际上,这一机制的最终目的是提供一个代码协同执行的支持机制。
以下内容为程序代码:
using System.Collections.Generic;
public class Tokens : IEnumerable
{
public IEnumerator GetEnumerator()
{
for(int i = 0; i
#include
#include
// The function object multiplies an element by a Factor
template
class MultValue
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue ( const Type& _Val