Fibers over Threads in D

99封情书 提交于 2019-12-10 12:47:01

问题


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)

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.

The documentation of D states:

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.

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.

Thanks in advance!


回答1:


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.



来源:https://stackoverflow.com/questions/7481498/fibers-over-threads-in-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!