gpt4 book ai didi

c# - 纤程与异步等待

转载 作者:可可西里 更新时间:2023-11-01 08:07:25 25 4
gpt4 key购买 nike

我正在加入一个 C# 项目,开发人员在其中大量使用 Fibers .在这个项目之前我什至没有听说过它们并且以前使用过 async awaitThreadsBackgroundWorker 来进行我的多任务操作。今天我问他们为什么使用 Fiber,主要开发人员说这样更容易调试。这意味着他知道特定函数来自哪个线程,甚至可以访问堆栈中更高层的变量。

我想知道使用 Fiber 与使用新的 async await 和使用 Thread 的优缺点是什么。

PS:我们使用的是 .Net 4.5

最佳答案

I was asking them why they used Fibers and the main developer said that it's easier for him to debug. Meaning he knows which thread a particular function has come from and even could access the variables higher in the stack.

这听起来很奇怪。将任务并行库与默认 ThreadPoolTask​​Scheduler 以外的自定义调度程序一起使用时,您可以自己决定任务的调度方式(不一定在新线程上)。另一方面,async-await 为您提供了一种执行异步 IO 的便捷方式。 VS 使您能够像同步执行一样调试异步代码。

为了使用纤程,必须调用非托管 API,因为 .NET 不在 BCL 中提供任何托管包装器。 Even the docs of fibers clearly say there isn't a clear advantage to using them :

In general, fibers do not provide advantages over a well-designed multithreaded application. However, using fibers can make it easier to port applications that were designed to schedule their own threads.


I was wondering what are the advantages and disadvantages of using Fibers vs using the new async await and using Threads.

使用 async-await 可以让您在执行 IO 绑定(bind)异步工作的同时感觉自己正在同步执行。 Task Parallel Library 提供了一种在专用线程上调度工作的简单方法,无论是线程池线程还是新线程,同时允许您 Hook 调度这些工作单元的机制。我真的看不出今天使用纤程有什么好处,因为框架必须提供所有功能。

我认为您应该告诉您的主要开发人员分别使用任务并行库和async-await 阅读有关多线程和异步 IO 工作的文章。我认为这会让你们所有人的生活更轻松。

关于c# - 纤程与异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com