What is the difference between a thread and a fiber?

前端 未结 9 900
北荒
北荒 2020-12-07 07: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

相关标签:
9条回答
  • 2020-12-07 08:00

    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.
    • 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.

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

    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.

    0 讨论(0)
  • 2020-12-07 08:00

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

    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.

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

    0 讨论(0)
  • 2020-12-07 08:02

    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.

    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.
    • 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.
    • SMT CPU can accept multiple threads and brake them into instruction fibers for parallel execution on multiple pipelines, using pipelines more efficiently.

    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.

    0 讨论(0)
提交回复
热议问题