线和纤维的区别是什么?我听说过来自红宝石的纤维,我也听说过它们在其他语言中也有,有人能简单地给我解释一下线和纤维的区别吗?


当前回答

首先,我建议阅读关于进程和线程之间区别的解释,作为背景材料。

一旦你读过了,这就很简单了。线程既可以在内核中实现,也可以在用户空间中实现,或者两者混合实现。光纤基本上是在用户空间中实现的线程。

What is typically called a thread is a thread of execution implemented in the kernel: what's known as a kernel thread. The scheduling of a kernel thread is handled exclusively by the kernel, although a kernel thread can voluntarily release the CPU by sleeping if it wants. A kernel thread has the advantage that it can use blocking I/O and let the kernel worry about scheduling. It's main disadvantage is that thread switching is relatively slow since it requires trapping into the kernel. Fibers are user space threads whose scheduling is handled in user space by one or more kernel threads under a single process. This makes fiber switching very fast. If you group all the fibers accessing a particular set of shared data under the context of a single kernel thread and have their scheduling handled by a single kernel thread, then you can eliminate synchronization issues since the fibers will effectively run in serial and you have complete control over their scheduling. Grouping related fibers under a single kernel thread is important, since the kernel thread they are running in can be pre-empted by the kernel. This point is not made clear in many of the other answers. Also, if you use blocking I/O in a fiber, the entire kernel thread it is a part of blocks including all the fibers that are part of that kernel thread.

在现代操作系统中的第11.4节“Windows Vista中的进程和线程”中,Tanenbaum评论道:

Although fibers are cooperatively scheduled, if there are multiple threads scheduling the fibers, a lot of careful synchronization is required to make sure fi­bers do not interfere with each other. To simplify the interaction between threads and fibers, it is often useful to create only as many threads as there are processors to run them, and affinitize the threads to each run only on a distinct set of avail­able processors, or even just one processor. Each thread can then run a particular subset of the fibers, establishing a one­ to-many relationship between threads and fibers which simplifies synchronization. Even so there are still many difficulties with fibers. Most Win32 libraries are completely unaware of fibers, and applications that attempt to use fibers as if they were threads will encounter various failures. The kernel has no knowledge of fi­bers, and when a fiber enters the kernel, the thread it is executing on may block and the kernel will schedule an arbitrary thread on the processor, making it unavailable to run other fibers. For these reasons fibers are rarely used except when porting code from other systems that explicitly need the functionality pro­vided by fibers.

其他回答

首先,我建议阅读关于进程和线程之间区别的解释,作为背景材料。

一旦你读过了,这就很简单了。线程既可以在内核中实现,也可以在用户空间中实现,或者两者混合实现。光纤基本上是在用户空间中实现的线程。

What is typically called a thread is a thread of execution implemented in the kernel: what's known as a kernel thread. The scheduling of a kernel thread is handled exclusively by the kernel, although a kernel thread can voluntarily release the CPU by sleeping if it wants. A kernel thread has the advantage that it can use blocking I/O and let the kernel worry about scheduling. It's main disadvantage is that thread switching is relatively slow since it requires trapping into the kernel. Fibers are user space threads whose scheduling is handled in user space by one or more kernel threads under a single process. This makes fiber switching very fast. If you group all the fibers accessing a particular set of shared data under the context of a single kernel thread and have their scheduling handled by a single kernel thread, then you can eliminate synchronization issues since the fibers will effectively run in serial and you have complete control over their scheduling. Grouping related fibers under a single kernel thread is important, since the kernel thread they are running in can be pre-empted by the kernel. This point is not made clear in many of the other answers. Also, if you use blocking I/O in a fiber, the entire kernel thread it is a part of blocks including all the fibers that are part of that kernel thread.

在现代操作系统中的第11.4节“Windows Vista中的进程和线程”中,Tanenbaum评论道:

Although fibers are cooperatively scheduled, if there are multiple threads scheduling the fibers, a lot of careful synchronization is required to make sure fi­bers do not interfere with each other. To simplify the interaction between threads and fibers, it is often useful to create only as many threads as there are processors to run them, and affinitize the threads to each run only on a distinct set of avail­able processors, or even just one processor. Each thread can then run a particular subset of the fibers, establishing a one­ to-many relationship between threads and fibers which simplifies synchronization. Even so there are still many difficulties with fibers. Most Win32 libraries are completely unaware of fibers, and applications that attempt to use fibers as if they were threads will encounter various failures. The kernel has no knowledge of fi­bers, and when a fiber enters the kernel, the thread it is executing on may block and the kernel will schedule an arbitrary thread on the processor, making it unavailable to run other fibers. For these reasons fibers are rarely used except when porting code from other systems that explicitly need the functionality pro­vided by fibers.

线程通常依赖内核来中断线程,以便它或另一个线程可以运行(这被称为抢占式多任务处理),而光纤使用合作多任务处理,在这种情况下,光纤本身放弃了自己的运行时间,以便其他光纤可以运行。

一些有用的链接可能比我解释得更好:

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

线程由操作系统调度(抢占式)。一个线程可以在任何时候被操作系统停止或恢复,但是纤维或多或少地管理自己(合作)并相互让步。也就是说,程序员控制光纤何时进行处理,以及该处理何时切换到另一条光纤。

用最简单的术语来说,线程通常被认为是抢占式的(尽管这可能并不总是正确的,这取决于操作系统),而光纤被认为是轻量级的协作线程。两者都是应用程序的独立执行路径。

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.

线程最初是作为轻量级进程创建的。以类似的方式,纤维是一种轻量级的线程,依靠(简单地说)纤维本身通过提供控制来相互调度。

我猜下一步将是每次你想让他们执行指令时,你都必须向他们发送一个信号(不像我5岁的儿子:-)。在过去(甚至现在在一些嵌入式平台上),所有的线程都是纤维,没有抢占,您必须编写线程以使其表现良好。