线和光纤有什么区别?

时间:2023-01-20 17:35:28

What is the difference between a thread and a fiber? I've heard of fibers from ruby and I've read heard they're available in other languages, could somebody explain to me in simple terms what is the difference between a thread and a fiber.

线和光纤有什么区别?我听说过来自红宝石的纤维,我读过听说它们可以用其他语言提供,有人可以用简单的语言向我解释一下螺纹和纤维之间有什么区别。

9 个解决方案

#1


In the most simple terms, threads are generally considered to be preemptive (although this may not always be true, depending on the operating system) while fibers are considered to be light-weight, cooperative threads. Both are separate execution paths for your application.

在最简单的术语中,线程通常被认为是抢占式的(虽然这可能并非总是如此,取决于操作系统),而光纤被认为是轻量级的协作线程。两者都是应用程序的独立执行路径。

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.

使用线程:当前执行路径可能随时被中断或抢占(注意:此语句是一般化,并且可能并不总是成立,具体取决于OS /线程包/等)。这意味着对于线程,数据完整性是一个大问题,因为在更新数据块的过程中可能会停止一个线程,从而使数据的完整性处于错误或不完整状态。这也意味着操作系统可以通过同时运行多个线程并将其留给开发人员来保护数据访问,从而利用多个CPU和CPU核心。

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.

对于光纤:当光纤执行时,当前执行路径仅中断(与上面相同的注释)。这意味着光纤总是在明确定义的位置启动和停止,因此数据完整性不是问题。此外,由于光纤通常在用户空间中进行管理,因此无需进行昂贵的上下文切换和CPU状态更改,从而使从一个光纤更改为下一个光纤非常有效。另一方面,由于没有两根光纤可以完全同时运行,仅仅使用光纤不会利用多个CPU或多个CPU内核。

#2


Threads use pre-emptive scheduling, whereas fibers use cooperative scheduling.

线程使用抢先调度,而光纤使用协作调度。

With a thread, the control flow could get interrupted at any time, and another thread can take over. With multiple processors, you can have multiple threads all running at the same time (simultaneous multithreading, or SMT). As a result, you have to be very careful about concurrent data access, and protect your data with mutexes, semaphores, condition variables, and so on. It is often very tricky to get right.

使用线程,控制流可以随时中断,另一个线程可以接管。使用多个处理器,您可以同时运行多个线程(同时多线程或SMT)。因此,您必须非常小心并发数据访问,并使用互斥锁,信号量,条件变量等保护数据。做对,通常很棘手。

With a fiber, control only switches when you tell it to, typically with a function call named something like yield(). This makes concurrent data access easier, since you don't have to worry about atomicity of data structures or mutexes. As long as you don't yield, there's no danger of being preempted and having another fiber trying to read or modify the data you're working with. As a result, though, if your fiber gets into an infinite loop, no other fiber can run, since you're not yielding.

使用光纤时,控制只会在您告诉它时切换,通常使用名为yield()的函数调用。这使得并发数据访问更容易,因为您不必担心数据结构或互斥体的原子性。只要你不屈服,就没有被抢占的危险,并且有另一根光纤试图读取或修改你正在使用的数据。因此,如果你的光纤进入无限循环,没有其他光纤可以运行,因为你没有屈服。

You can also mix threads and fibers, which gives rise to the problems faced by both. Not recommended, but it can sometimes be the right thing to do if done carefully.

您还可以混合线和纤维,这会产生两者所面临的问题。不推荐,但如果仔细进行,有时可能是正确的做法。

#3


In Win32, a fiber is a sort of user-managed thread. A fiber has its own stack and its own instruction pointer etc., but fibers are not scheduled by the OS: you have to call SwitchToFiber explicitly. Threads, by contrast, are pre-emptively scheduled by the operation system. So roughly speaking a fiber is a thread that is managed at the application/runtime level rather than being a true OS thread.

在Win32中,光纤是一种用户管理的线程。光纤有自己的堆栈和自己的指令指针等,但操作系统不会调度光纤:您必须显式调用SwitchToFiber。相反,线程由操作系统预先安排。粗略地说,光纤是一个在应用程序/运行时级别管理的线程,而不是真正的操作系统线程。

The consequences are that fibers are cheaper and that the application has more control over scheduling. This can be important if the app creates a lot of concurrent tasks, and/or wants to closely optimise when they run. For example, a database server might choose to use fibers rather than threads.

结果是光纤更便宜并且应用程序对调度具有更多控制。如果应用程序创建了大量并发任务,并且/或者希望在运行时进行密切优化,这一点非常重要。例如,数据库服务器可能选择使用光纤而不是线程。

(There may be other usages for the same term; as noted, this is the Win32 definition.)

(对于同一术语,可能还有其他用法;如上所述,这是Win32定义。)

#4


First I would recommend reading this explanation of the difference between processes and threads as background material.

首先,我建议阅读过程和线程之间差异的这种解释作为背景材料。

Once you've read that it's pretty straight forward. Threads cans be implemented either in the kernel, in user space, or the two can be mixed. Fibers are basically threads implemented in user space.

一旦你读完它就会很直接。线程可以在内核,用户空间中实现,也可以两者混合使用。光纤基本上是在用户空间中实现的线程。

  • 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.
  • 通常称为线程的是在内核中实现的执行线程:所谓的内核线程。内核线程的调度由内核专门处理,尽管内核线程可以根据需要通过休眠自愿释放CPU。内核线程的优点是它可以使用阻塞I / O并让内核担心调度。它的主要缺点是线程切换相对较慢,因为它需要捕获到内核中。

  • 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.
  • Fibers是用户空间线程,其调度在一个进程下由一个或多个内核线程在用户空间中处理。这使得光纤切换速度非常快。如果将所有光纤分组在单个内核线程的上下文中访问一组特定的共享数据,并将其调度由单个内核线程处理,则可以消除同步问题,因为光纤将有效地串行运行并且您已完成控制他们的日程安排。在单个内核线程下对相关光纤进行分组很重要,因为它们运行的​​内核线程可以被内核抢占。在许多其他答案中,这一点并未明确。此外,如果在光纤中使用阻塞I / O,则整个内核线程它是块的一部分,包括作为该内核线程一部分的所有光纤。

In section 11.4 "Processes and Threads in Windows Vista" in Modern Operating Systems, Tanenbaum comments:

在现代操作系统的第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.

尽管光纤是协同安排的,但如果有多个线程调度光纤,则需要进行大量的仔细同步以确保光纤不会相互干扰。为了简化线程和光纤之间的交互,通常只需创建与运行它们的处理器一样多的线程,并将线程关联到每个运行仅在一组不同的可用处理器上,或者甚至只是一个处理器上。然后,每个线程可以运行光纤的特定子集,在线程和光纤之间建立一对多关系,这简化了同步。即便如此,纤维仍存在许多困难。大多数Win32库完全没有意识到光纤,尝试使用光纤的应用程序就像它们是线程一样会遇到各种故障。内核不了解光纤,当光纤进入内核时,它正在执行的线程可能会阻塞,内核将在处理器上调度任意线程,使其无法运行其他光纤。由于这些原因,很少使用光纤,除非从明确需要光纤提供的功能的其他系统移植代码。

#5


Note that in addition to Threads and Fibers, Windows 7 introduces User-Mode Scheduling:

请注意,除了线程和光纤之外,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)是一种轻量级机制,应用程序可以使用它来调度自己的线程。应用程序可以在用户模式下在UMS线程之间切换,而不涉及系统调度程序,并且如果UMS线程在内核中阻塞,则重新获得对处理器的控制。 UMS线程与光纤的不同之处在于每个UMS线程都有自己的线程上下文,而不是共享单个线程的线程上下文。在用户模式下在线程之间切换的能力使UMS比线程池更有效,用于管理需要很少系统调用的大量短期工作项。

More information about threads, fibers and UMS is available by watching Dave Probert: Inside Windows 7 - User Mode Scheduler (UMS).

有关线程,光纤和UMS的更多信息,请参阅Dave Probert:Inside Windows 7 - 用户模式调度程序(UMS)。

#6


Threads are scheduled by the OS (pre-emptive). A thread may be stopped or resumed at any time by the OS, but fibers more or less manage themselves (co-operative) and yield to each other. That is, the programmer controls when fibers do their processing and when that processing switches to another fiber.

线程由OS调度(先发制人)。 OS可以随时停止或恢复线程,但是光纤或多或少地管理自己(合作)并相互屈服。也就是说,程序员控制光纤何时进行处理以及何时处理切换到另一根光纤。

#7


Threads were originally created as lightweight processes. In a similar fashion, fibers are a lightweight thread, relying (simplistically) on the fibers themselves to schedule each other, by yielding control.

线程最初是作为轻量级进程创建的。以类似的方式,纤维是轻质的线,依靠(简单地)依赖于纤维本身以通过控制来彼此安排。

I guess the next step will be strands where you have to send them a signal every time you want them to execute an instruction (not unlike my 5yo son :-). In the old days (and even now on some embedded platforms), all threads were fibers, there was no pre-emption and you had to write your threads to behave nicely.

我想下一步将是你必须每次希望他们执行指令时发送信号的链(不像我的5yo儿子:-)。在过去(甚至现在在一些嵌入式平台上),所有线程都是光纤,没有先发制人,你必须编写你的线程以表现得很好。

#8


Threads generally rely on the kernel to interrupt the thread so it or another thread can run (which is better known as Pre-emptive multitasking) whereas fibers use co-operative multitasking where it is the fiber itself that give up the its running time so that other fibres can run.

线程通常依赖于内核来中断线程,因此它或其他线程可以运行(这更好地称为抢占式多任务处理),而光纤使用协作式多任务处理,其中光纤本身放弃其运行时间,以便其他纤维可以运行。

Some useful links explaining it better than I probably did are:

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

#9


Win32 fiber definition is in fact "Green Thread" definition established at Sun Microsystems. There is no need to waste the term fiber on the thread of some kind, i.e., a thread executing in user space under user code/thread-library control.

Win32光纤定义实际上是在Sun Microsystems建立的“绿线”定义。不需要在某种线程上浪费术语光纤,即在用户代码/线程库控制下在用户空间中执行的线程。

To clarify the argument look at the following comments:

为澄清论点,请看以下评论​​:

  • With hyper-threading, multi-core CPU can accept multiple threads and distribute them one on each core.
  • 通过超线程,多核CPU可以接受多个线程并在每个核心上分配一个线程。

  • Superscalar pipelined CPU accepts one thread for execution and uses Instruction Level Parallelism (ILP) to to run the the thread faster. We may assume that one thread is broken into parallel fibers running in parallel pipelines.
  • 超标量流水线CPU接受一个线程执行,并使用指令级并行(ILP)来更快地运行线程。我们可以假设一个线程被分解为在并行管道中运行的并行光纤。

  • SMT CPU can accept multiple threads and brake them into instruction fibers for parallel execution on multiple pipelines, using pipelines more efficiently.
  • SMT CPU可以接受多个线程并将它们制成指令光纤,以便在多个流水线上并行执行,从而更有效地使用流水线。

We should assume that processes are made of threads and that threads should be made of fibers. With that logic in mind, using fibers for other sorts of threads is wrong.

我们应该假设过程是由线程组成的,并且线程应该由纤维制成。考虑到这一逻辑,将纤维用于其他种类的线程是错误的。

#1


In the most simple terms, threads are generally considered to be preemptive (although this may not always be true, depending on the operating system) while fibers are considered to be light-weight, cooperative threads. Both are separate execution paths for your application.

在最简单的术语中,线程通常被认为是抢占式的(虽然这可能并非总是如此,取决于操作系统),而光纤被认为是轻量级的协作线程。两者都是应用程序的独立执行路径。

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.

使用线程:当前执行路径可能随时被中断或抢占(注意:此语句是一般化,并且可能并不总是成立,具体取决于OS /线程包/等)。这意味着对于线程,数据完整性是一个大问题,因为在更新数据块的过程中可能会停止一个线程,从而使数据的完整性处于错误或不完整状态。这也意味着操作系统可以通过同时运行多个线程并将其留给开发人员来保护数据访问,从而利用多个CPU和CPU核心。

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.

对于光纤:当光纤执行时,当前执行路径仅中断(与上面相同的注释)。这意味着光纤总是在明确定义的位置启动和停止,因此数据完整性不是问题。此外,由于光纤通常在用户空间中进行管理,因此无需进行昂贵的上下文切换和CPU状态更改,从而使从一个光纤更改为下一个光纤非常有效。另一方面,由于没有两根光纤可以完全同时运行,仅仅使用光纤不会利用多个CPU或多个CPU内核。

#2


Threads use pre-emptive scheduling, whereas fibers use cooperative scheduling.

线程使用抢先调度,而光纤使用协作调度。

With a thread, the control flow could get interrupted at any time, and another thread can take over. With multiple processors, you can have multiple threads all running at the same time (simultaneous multithreading, or SMT). As a result, you have to be very careful about concurrent data access, and protect your data with mutexes, semaphores, condition variables, and so on. It is often very tricky to get right.

使用线程,控制流可以随时中断,另一个线程可以接管。使用多个处理器,您可以同时运行多个线程(同时多线程或SMT)。因此,您必须非常小心并发数据访问,并使用互斥锁,信号量,条件变量等保护数据。做对,通常很棘手。

With a fiber, control only switches when you tell it to, typically with a function call named something like yield(). This makes concurrent data access easier, since you don't have to worry about atomicity of data structures or mutexes. As long as you don't yield, there's no danger of being preempted and having another fiber trying to read or modify the data you're working with. As a result, though, if your fiber gets into an infinite loop, no other fiber can run, since you're not yielding.

使用光纤时,控制只会在您告诉它时切换,通常使用名为yield()的函数调用。这使得并发数据访问更容易,因为您不必担心数据结构或互斥体的原子性。只要你不屈服,就没有被抢占的危险,并且有另一根光纤试图读取或修改你正在使用的数据。因此,如果你的光纤进入无限循环,没有其他光纤可以运行,因为你没有屈服。

You can also mix threads and fibers, which gives rise to the problems faced by both. Not recommended, but it can sometimes be the right thing to do if done carefully.

您还可以混合线和纤维,这会产生两者所面临的问题。不推荐,但如果仔细进行,有时可能是正确的做法。

#3


In Win32, a fiber is a sort of user-managed thread. A fiber has its own stack and its own instruction pointer etc., but fibers are not scheduled by the OS: you have to call SwitchToFiber explicitly. Threads, by contrast, are pre-emptively scheduled by the operation system. So roughly speaking a fiber is a thread that is managed at the application/runtime level rather than being a true OS thread.

在Win32中,光纤是一种用户管理的线程。光纤有自己的堆栈和自己的指令指针等,但操作系统不会调度光纤:您必须显式调用SwitchToFiber。相反,线程由操作系统预先安排。粗略地说,光纤是一个在应用程序/运行时级别管理的线程,而不是真正的操作系统线程。

The consequences are that fibers are cheaper and that the application has more control over scheduling. This can be important if the app creates a lot of concurrent tasks, and/or wants to closely optimise when they run. For example, a database server might choose to use fibers rather than threads.

结果是光纤更便宜并且应用程序对调度具有更多控制。如果应用程序创建了大量并发任务,并且/或者希望在运行时进行密切优化,这一点非常重要。例如,数据库服务器可能选择使用光纤而不是线程。

(There may be other usages for the same term; as noted, this is the Win32 definition.)

(对于同一术语,可能还有其他用法;如上所述,这是Win32定义。)

#4


First I would recommend reading this explanation of the difference between processes and threads as background material.

首先,我建议阅读过程和线程之间差异的这种解释作为背景材料。

Once you've read that it's pretty straight forward. Threads cans be implemented either in the kernel, in user space, or the two can be mixed. Fibers are basically threads implemented in user space.

一旦你读完它就会很直接。线程可以在内核,用户空间中实现,也可以两者混合使用。光纤基本上是在用户空间中实现的线程。

  • 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.
  • 通常称为线程的是在内核中实现的执行线程:所谓的内核线程。内核线程的调度由内核专门处理,尽管内核线程可以根据需要通过休眠自愿释放CPU。内核线程的优点是它可以使用阻塞I / O并让内核担心调度。它的主要缺点是线程切换相对较慢,因为它需要捕获到内核中。

  • 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.
  • Fibers是用户空间线程,其调度在一个进程下由一个或多个内核线程在用户空间中处理。这使得光纤切换速度非常快。如果将所有光纤分组在单个内核线程的上下文中访问一组特定的共享数据,并将其调度由单个内核线程处理,则可以消除同步问题,因为光纤将有效地串行运行并且您已完成控制他们的日程安排。在单个内核线程下对相关光纤进行分组很重要,因为它们运行的​​内核线程可以被内核抢占。在许多其他答案中,这一点并未明确。此外,如果在光纤中使用阻塞I / O,则整个内核线程它是块的一部分,包括作为该内核线程一部分的所有光纤。

In section 11.4 "Processes and Threads in Windows Vista" in Modern Operating Systems, Tanenbaum comments:

在现代操作系统的第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.

尽管光纤是协同安排的,但如果有多个线程调度光纤,则需要进行大量的仔细同步以确保光纤不会相互干扰。为了简化线程和光纤之间的交互,通常只需创建与运行它们的处理器一样多的线程,并将线程关联到每个运行仅在一组不同的可用处理器上,或者甚至只是一个处理器上。然后,每个线程可以运行光纤的特定子集,在线程和光纤之间建立一对多关系,这简化了同步。即便如此,纤维仍存在许多困难。大多数Win32库完全没有意识到光纤,尝试使用光纤的应用程序就像它们是线程一样会遇到各种故障。内核不了解光纤,当光纤进入内核时,它正在执行的线程可能会阻塞,内核将在处理器上调度任意线程,使其无法运行其他光纤。由于这些原因,很少使用光纤,除非从明确需要光纤提供的功能的其他系统移植代码。

#5


Note that in addition to Threads and Fibers, Windows 7 introduces User-Mode Scheduling:

请注意,除了线程和光纤之外,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)是一种轻量级机制,应用程序可以使用它来调度自己的线程。应用程序可以在用户模式下在UMS线程之间切换,而不涉及系统调度程序,并且如果UMS线程在内核中阻塞,则重新获得对处理器的控制。 UMS线程与光纤的不同之处在于每个UMS线程都有自己的线程上下文,而不是共享单个线程的线程上下文。在用户模式下在线程之间切换的能力使UMS比线程池更有效,用于管理需要很少系统调用的大量短期工作项。

More information about threads, fibers and UMS is available by watching Dave Probert: Inside Windows 7 - User Mode Scheduler (UMS).

有关线程,光纤和UMS的更多信息,请参阅Dave Probert:Inside Windows 7 - 用户模式调度程序(UMS)。

#6


Threads are scheduled by the OS (pre-emptive). A thread may be stopped or resumed at any time by the OS, but fibers more or less manage themselves (co-operative) and yield to each other. That is, the programmer controls when fibers do their processing and when that processing switches to another fiber.

线程由OS调度(先发制人)。 OS可以随时停止或恢复线程,但是光纤或多或少地管理自己(合作)并相互屈服。也就是说,程序员控制光纤何时进行处理以及何时处理切换到另一根光纤。

#7


Threads were originally created as lightweight processes. In a similar fashion, fibers are a lightweight thread, relying (simplistically) on the fibers themselves to schedule each other, by yielding control.

线程最初是作为轻量级进程创建的。以类似的方式,纤维是轻质的线,依靠(简单地)依赖于纤维本身以通过控制来彼此安排。

I guess the next step will be strands where you have to send them a signal every time you want them to execute an instruction (not unlike my 5yo son :-). In the old days (and even now on some embedded platforms), all threads were fibers, there was no pre-emption and you had to write your threads to behave nicely.

我想下一步将是你必须每次希望他们执行指令时发送信号的链(不像我的5yo儿子:-)。在过去(甚至现在在一些嵌入式平台上),所有线程都是光纤,没有先发制人,你必须编写你的线程以表现得很好。

#8


Threads generally rely on the kernel to interrupt the thread so it or another thread can run (which is better known as Pre-emptive multitasking) whereas fibers use co-operative multitasking where it is the fiber itself that give up the its running time so that other fibres can run.

线程通常依赖于内核来中断线程,因此它或其他线程可以运行(这更好地称为抢占式多任务处理),而光纤使用协作式多任务处理,其中光纤本身放弃其运行时间,以便其他纤维可以运行。

Some useful links explaining it better than I probably did are:

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

#9


Win32 fiber definition is in fact "Green Thread" definition established at Sun Microsystems. There is no need to waste the term fiber on the thread of some kind, i.e., a thread executing in user space under user code/thread-library control.

Win32光纤定义实际上是在Sun Microsystems建立的“绿线”定义。不需要在某种线程上浪费术语光纤,即在用户代码/线程库控制下在用户空间中执行的线程。

To clarify the argument look at the following comments:

为澄清论点,请看以下评论​​:

  • With hyper-threading, multi-core CPU can accept multiple threads and distribute them one on each core.
  • 通过超线程,多核CPU可以接受多个线程并在每个核心上分配一个线程。

  • Superscalar pipelined CPU accepts one thread for execution and uses Instruction Level Parallelism (ILP) to to run the the thread faster. We may assume that one thread is broken into parallel fibers running in parallel pipelines.
  • 超标量流水线CPU接受一个线程执行,并使用指令级并行(ILP)来更快地运行线程。我们可以假设一个线程被分解为在并行管道中运行的并行光纤。

  • SMT CPU can accept multiple threads and brake them into instruction fibers for parallel execution on multiple pipelines, using pipelines more efficiently.
  • SMT CPU可以接受多个线程并将它们制成指令光纤,以便在多个流水线上并行执行,从而更有效地使用流水线。

We should assume that processes are made of threads and that threads should be made of fibers. With that logic in mind, using fibers for other sorts of threads is wrong.

我们应该假设过程是由线程组成的,并且线程应该由纤维制成。考虑到这一逻辑,将纤维用于其他种类的线程是错误的。