class Fiber
- Fiber
- Reference
- Object
Overview
A Fiber
is a light-weight execution unit managed by the Crystal runtime.
It is conceptually similar to an operating system thread but with less overhead and completely internal to the Crystal process. The runtime includes a scheduler which schedules execution of fibers.
A Fiber
has a stack size of 8 MiB
which is usually also assigned
to an operating system thread. But only 4KiB
are actually allocated at first
so the memory footprint is very small.
Communication between fibers is usually passed through Channel
.
Cooperative
Fibers are cooperative. That means execution can only be drawn from a fiber when it offers it. It can't be interrupted in its execution at random. In order to make concurrency work, fibers must make sure to occasionally provide hooks for the scheduler to swap in other fibers. IO operations like reading from a file descriptor are natural implementations for this and the developer does not need to take further action on that. When IO access can't be served immediately by a buffer, the fiber will automatically wait and yield execution. When IO is ready it's going to be resumed through the event loop.
When a computation-intensive task has none or only rare IO operations, a fiber
should explicitly offer to yield execution from time to time using
Fiber.yield
to break up tight loops. The frequency of this call depends on
the application and concurrency model.
Event loop
The event loop is responsible for keeping track of sleeping fibers waiting for notifications that IO is ready or a timeout reached. When a fiber can be woken, the event loop enqueues it in the scheduler
Defined in:
fiber_metrics/call_track.crfiber_metrics/fiber.cr
fiber_metrics/printer/tallboy.cr
Class Method Summary
-
.print_stats(io = STDOUT) : Nil
- An empty space means 0.
EXPERIMENTAL
-
.stats
EXPERIMENTAL
Macro Summary
-
measure(name = nil)
EXPERIMENTAL
-
measure_blocking(name = nil)
EXPERIMENTAL
-
measure_idle(name = nil)
EXPERIMENTAL
Class Method Detail
- An empty space means 0. For memory this means no allocations
- 0K or 0.000 means < half the number specified. For memory it means less than 0.5K
EXPERIMENTAL