在C#中的协同程序 [英] Coroutines in C#

查看:728
本文介绍了在C#中的协同程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在C#的方式来实现协同例程(用户调度的线程)。当用c ++我用纤维。我在网上看到纤维在C#中不存在。我想获得类似的功能。

I am looking at ways to implement co-routines (user scheduled threads) in c#. When using c++ I was using fibers. I see on the internet fibers do not exist in C#. I would like to get similar functionality.

有没有正确的方式来实现在C#中的协同程序?

Is there any "right" way to implement coroutines in c#?

我也曾想过实施这个使用线程获取有关调度程序线程一个执行互斥+ 1,它释放此互斥每个协程。但这似乎非常昂贵的(它迫使每个协同程序之间的上下文切换)

I have thought of implementing this using threads that acquire a single execution mutex + 1 on scheduler thread which releases this mutex for each coroutine. But this seems very costly (it forces a context switch between each coroutine)

我也看到了产量迭代器的功能,但据我所知,你不能在一个屈服内部函数(仅在原始的IEnumerator功能)。所以这确实有点我好

I have also seen the yield iterator functionality, but as I understand you can't yield within an internal function (only in the original ienumerator function). So this does me little good.

推荐答案

我认为,你应该看的的Reactive扩展为.NET 。例如语句来模拟协同程序href=\"http://en.wikipedia.org/wiki/Coroutine#Implementations_for_C.23\">

I believe that you should look at the the Reactive Extensions for .NET. For example coroutines can be simulated using iterators and the yield statement.

然而,你可能需要阅读此 SO质疑了。

However you may want to read this SO question too.

这篇关于在C#中的协同程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆