线和纤维的区别是什么?我听说过来自红宝石的纤维,我也听说过它们在其他语言中也有,有人能简单地给我解释一下线和纤维的区别吗?
当前回答
用最简单的术语来说,线程通常被认为是抢占式的(尽管这可能并不总是正确的,这取决于操作系统),而光纤被认为是轻量级的协作线程。两者都是应用程序的独立执行路径。
With threads: the current execution path may be interrupted or preempted at any time (note: this statement is a generalization and may not always hold true depending on OS/threading package/etc.). This means that for threads, data integrity is a big issue because one thread may be stopped in the middle of updating a chunk of data, leaving the integrity of the data in a bad or incomplete state. This also means that the operating system can take advantage of multiple CPUs and CPU cores by running more than one thread at the same time and leaving it up to the developer to guard data access.
With fibers: the current execution path is only interrupted when the fiber yields execution (same note as above). This means that fibers always start and stop in well-defined places, so data integrity is much less of an issue. Also, because fibers are often managed in the user space, expensive context switches and CPU state changes need not be made, making changing from one fiber to the next extremely efficient. On the other hand, since no two fibers can run at exactly the same time, just using fibers alone will not take advantage of multiple CPUs or multiple CPU cores.
其他回答
线程使用抢占式调度,而光纤使用协作式调度。
对于一个线程,控制流可能在任何时候被中断,而另一个线程可以接管。使用多个处理器,可以同时运行多个线程(同步多线程,或SMT)。因此,您必须非常小心并发数据访问,并使用互斥锁、信号量、条件变量等保护数据。这通常是非常棘手的。
对于光纤,控制只在您告诉它切换时才切换,通常使用名为yield()的函数调用。这使得并发数据访问更容易,因为您不必担心数据结构或互斥对象的原子性。只要您不让步,就不会有被抢占的危险,也不会有另一条光纤试图读取或修改您正在处理的数据。因此,如果你的纤维进入了一个无限循环,其他的纤维就无法运行,因为你没有屈服。
您还可以混合使用线程和纤维,这会导致两者都面临的问题。不建议这样做,但如果仔细的话,有时这样做是正确的。
线程通常依赖内核来中断线程,以便它或另一个线程可以运行(这被称为抢占式多任务处理),而光纤使用合作多任务处理,在这种情况下,光纤本身放弃了自己的运行时间,以便其他光纤可以运行。
一些有用的链接可能比我解释得更好:
http://en.wikipedia.org/wiki/Fiber_ (computer_science) http://en.wikipedia.org/wiki/Computer_multitasking#Cooperative_multitasking.2Ftime-sharing http://en.wikipedia.org/wiki/Pre-emptive_multitasking
注意,除了线程和光纤,Windows 7还引入了用户模式调度:
User-mode scheduling (UMS) is a light-weight mechanism that applications can use to schedule their own threads. An application can switch between UMS threads in user mode without involving the system scheduler and regain control of the processor if a UMS thread blocks in the kernel. UMS threads differ from fibers in that each UMS thread has its own thread context instead of sharing the thread context of a single thread. The ability to switch between threads in user mode makes UMS more efficient than thread pools for managing large numbers of short-duration work items that require few system calls.
有关线程、光纤和UMS的更多信息,请参见Dave Probert: Windows 7内部-用户模式调度器(UMS)。
线程由操作系统调度(抢占式)。一个线程可以在任何时候被操作系统停止或恢复,但是纤维或多或少地管理自己(合作)并相互让步。也就是说,程序员控制光纤何时进行处理,以及该处理何时切换到另一条光纤。
线程最初是作为轻量级进程创建的。以类似的方式,纤维是一种轻量级的线程,依靠(简单地说)纤维本身通过提供控制来相互调度。
我猜下一步将是每次你想让他们执行指令时,你都必须向他们发送一个信号(不像我5岁的儿子:-)。在过去(甚至现在在一些嵌入式平台上),所有的线程都是纤维,没有抢占,您必须编写线程以使其表现良好。
推荐文章
- 跨线程操作无效:控件“textBox1”从创建它的线程以外的线程访问
- 线和纤维的区别是什么?
- Java同步方法锁定对象,或方法?
- Haskell对Node.js的响应是什么?
- 调用线程必须是STA,因为许多UI组件都要求这一点
- Redis是单线程的,那么它如何做并发I/O?
- Java:如何测试调用System.exit()的方法?
- 不能pickle <type 'instancemethod'>当使用多处理Pool.map()
- UI线程上的任务继续
- ExecutorService,如何等待所有任务完成
- 在python中创建线程
- 处理来自Java ExecutorService任务的异常
- c#事件和线程安全
- Thread start()和Runnable run()有什么区别
- 如何在Python中获取线程id ?