D中的螺纹上的光纤 [英] Fibers over Threads in D

查看:67
本文介绍了D中的螺纹上的光纤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在D中测试线程和Fiber,我想知道在主线程运行时是否可以在其他CPU上运行Fiber。如果不是这种情况,那么使用基于线程的光纤的原因将是什么。 (非常欢迎实际的例子)

I'm experimenting with threads and Fibers in D and I was wondering if it is possible to run a Fiber on a different CPU as the main thread is running. And if this is not the case then what would be the reason of using Fibers over Threads. (Practical examples are very welcome)

我试图用Fibers编写一些初始程序,一段时间后我切换到下一个光纤。但是,我注意到cpu的使用仅停留在一个cpu上。

I tried to write some initial program with Fibers where I switch to the next fiber after some time. Howhever I noticed that the cpu usage stays only on one cpu.

D的文档指出:


请注意,不要求将光纤绑定到一个
特定线程。相反,只要它们当前不执行,光纤就可以在线程
之间自由传递。

Please note that there is no requirement that a fiber be bound to one specific thread. Rather, fibers may be freely passed between threads so long as they are not currently executing.

这是否意味着我如果我要使用其他CPU,必须为光纤提供运行线程吗?如果是这种情况,那么我看不到目的。

Does this mean that I have to provide a thread for the fiber to run on if i want it to use a different CPU ? If this is the case then I don't see the purpose.

预先感谢!

推荐答案

纤维是用于协作多任务的轻量级机制,并在与其创建者/调用者相同的线程中运行。如果需要在其他CPU上运行任务,请使用线程。光纤的目的是提供快速的协作上下文切换以及用于实现诸如协程的模式的机制。

Fibers are a lightweight mechanism for cooperative multitasking, and run in the same thread as their creator / caller. If you need to run a task on a different CPU, use threads. The purpose of fibers is to provide fast cooperative context switching and mechanisms for implementing patterns such as coroutines.

这篇关于D中的螺纹上的光纤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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