ArticleslgStudy

computer science

Fiber (computer science)

Fiber (computer science) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Fiber (computer science) rather than just read about it. In short: In computer science, a fiber is a particularly lightweight thread of execution. Like threads, fibers share address space.

Key takeaways

  • Fiber (computer science) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Fiber (computer science) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Fiber (computer science) from memory before moving on to harder problems.

Reference excerpt

In computer science, a fiber is a particularly lightweight thread of execution. Like threads, fibers share address space. However, fibers use cooperative multitasking while threads use preemptive multitasking. Threads often depend on the kernel's thread scheduler to preempt a busy thread and resume another thread; fibers yield themselves to run another fiber while executing.

Threads, fibers and coroutines The key difference between fibers and kernel threads is that fibers use cooperative context switching, instead of preemptive time-slicing. In effect, fibers extend the concurrency taxonomy:

on a single computer, multiple processes can run within a single process, multiple threads can run within a single thread, multiple fibers can run Fibers (sometimes called stackful coroutines or user mode cooperatively scheduled threads) and stackless coroutines (compiler synthesized state machines) represent two distinct programming facilities with vast performance and functionality differences.

Advantages and disadvantages Because fibers multitask cooperatively, thread safety is less of an issue than with preemptively scheduled threads, and synchronization constructs including spinlocks and atomic operations are unnecessary when writing fibered code, as they are implicitly synchronized. However, many libraries yield a fiber implicitly as a method of conducting non-blocking I/O; as such, some caution and documentation reading is advised. A disadvantage is that fibers cannot utilize multiprocessor machines without also using preemptive threads; however, an M:N threading model with no more preemptive threads than CPU cores can be more efficient than either pure fibers or pure preemptive threading. In some server programs, fibers are used to soft block themselves to allow their single-threaded parent programs to continue working. In this design, fibers are used mostly for I/O access which does not need CPU processing. This allows the main program to continue with what it is doing. Fibers yield control to the single-threaded main program, and when the I/O operation is completed fibers continue where they left off.

Operating system support Less support from the operating system is needed for fibers than for threads. They can be implemented in modern Unix systems using the library functions getcontext, setcontext and swapcontext in <ucontext.h>, as in GNU Portable Threads, or in assembler as boost::fibers. On Microsoft Windows, fibers are created using the ConvertThreadToFiber and CreateFiber calls; a fiber that is currently suspended may be resumed in any thread. Fiber-local storage, analogous to thread-local storage, may be used to create unique copies of variables. Symbian OS used a similar concept to fibers in its Active Scheduler. An active object contained one fiber to be executed by the Active Scheduler when one of several outstanding asynchronous calls completed. Several Active objects could be waiting to execute (based on priority) and each one had to restrict its own execution time.

Fiber implementation examples Fibers can be implemented without operating system support, although some operating systems or libraries provide explicit support for them.

Win32 supplies a fiber API (Windows NT 3.51 SP3 and later) Boost (C++) has a boost::fibers::fiber class since Boost version 1.62 D has a class core.thread.fiber.Fiber in its standard library. Ruby had Green threads (before version 1.9) Netscape Portable Runtime (includes a user-space fibers implementation) ribs2 PHP since version 8.1 Rust fibers that use Futures under the hood Crystal provides fibers as a part of the language and standard library

See also setcontext/getcontext library routines Green threads and virtual threads call-with-current-continuation

References

External links GNU Portable threads "Portable Coroutine Library". Freecode. Fiber Pool A multicore-capable C++ framework based on fibers for Microsoft Windows. State Threads Protothreads ribs2 boost.fiber

Worked examples

Example 1 — a first encounter with Fiber (computer science)

Start with the simplest possible case. Write down what Fiber (computer science) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Fiber (computer science) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Fiber (computer science) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Fiber (computer science)

In research
Fiber (computer science) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Fiber (computer science) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Fiber (computer science) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Control flow, Threads (computing), so understanding it makes those chapters shorter.
In everyday life
Look for Fiber (computer science) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Fiber (computer science)” →

How to study Fiber (computer science) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Fiber (computer science) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Fiber (computer science) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Fiber (computer science) in simple terms?

In computer science, a fiber is a particularly lightweight thread of execution. Like threads, fibers share address space.

Why does Fiber (computer science) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Fiber (computer science)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Fiber (computer science).

Tags

  • Control flow
  • Threads (computing)

Keep exploring