]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/fiber/doc/overview.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / fiber / doc / overview.qbk
CommitLineData
7c673cae
FG
1[/
2 Copyright Oliver Kowalke 2013.
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt
6]
7
8[section:overview Overview]
9
10__boost_fiber__ provides a framework for micro-/userland-threads (fibers)
11scheduled cooperatively.
12The API contains classes and functions to manage and synchronize fibers similiarly to
13__std_thread__.
14
15Each fiber has its own stack.
16
17A fiber can save the current execution state, including all registers
18and CPU flags, the instruction pointer, and the stack pointer and later restore
19this state.
20The idea is to have multiple execution paths running on a single thread using
21cooperative scheduling (versus threads, which are preemptively scheduled). The
22running fiber decides explicitly when it should yield to allow another fiber to
23run (context switching).
24__boost_fiber__ internally uses __econtext__ from __boost_context__; the classes in
25this library manage, schedule and, when needed, synchronize those execution contexts.
26A context switch between threads usually costs thousands of CPU cycles on x86,
27compared to a fiber switch with less than a hundred cycles.
28A fiber runs on a single thread at any point in time.
29
30In order to use the classes and functions described here, you can either include
31the specific headers specified by the descriptions of each class or function, or
32include the master library header:
33
34 #include <boost/fiber/all.hpp>
35
36which includes all the other headers in turn.
37
38The namespaces used are:
39
40 namespace boost::fibers
41 namespace boost::this_fiber
42
43[heading Fibers and Threads]
44
45Control is cooperatively passed between fibers launched on a given thread. At
46a given moment, on a given thread, at most one fiber is running.
47
48Spawning additional fibers on a given thread does not distribute your program
49across more hardware cores, though it can make more effective use of the core
50on which it's running.
51
52On the other hand, a fiber may safely access any resource exclusively owned by
53its parent thread without explicitly needing to defend that resource against
54concurrent access by other fibers on the same thread. You are already
55guaranteed that no other fiber on that thread is concurrently touching that
56resource. This can be particularly important when introducing concurrency in
57legacy code. You can safely spawn fibers running old code, using asynchronous
58I/O to interleave execution.
59
60In effect, fibers provide a natural way to organize concurrent code based on
61asynchronous I/O. Instead of chaining together completion handlers, code
62running on a fiber can make what looks like a normal blocking function call.
63That call can cheaply suspend the calling fiber, allowing other fibers on the
64same thread to run. When the operation has completed, the suspended fiber
65resumes, without having to explicitly save or restore its state. Its local
66stack variables persist across the call.
67
68A fiber can be migrated from one thread to another, though the library does
69not do this by default. It is possible for you to supply a custom scheduler
70that migrates fibers between threads. You may specify custom fiber properties
71to help your scheduler decide which fibers are permitted to migrate. Please
72see [link migration Migrating fibers between threads] and [link custom
73Customization] for more details.
74
75A fiber launched on a particular thread continues running on that thread
76unless migrated. It might be unblocked (see [link blocking Blocking] below) by
77some other thread, but that only transitions the fiber from ["blocked] to
78["ready] on its current thread [mdash] it does not cause the fiber to
79resume on the thread that unblocked it.
80
81[#thread_local_storage]
82[heading thread-local storage]
83Unless migrated, a fiber may access thread-local storage; however that storage
84will be shared among all fibers running on the same thread. For fiber-local
85storage, please see __fsp__.
86
87[#cross_thread_sync]
88[heading BOOST_FIBERS_NO_ATOMICS]
89The fiber synchronization objects provided by this library will, by default,
90safely synchronize fibers running on different threads. However, this level of
91synchronization can be removed (for performance) by building the library with
92[*`BOOST_FIBERS_NO_ATOMICS`] defined. When the library is built with that macro,
93you must ensure that all the fibers referencing a particular synchronization
94object are running in the same thread. Please see [link synchronization
95Synchronization].
96
97[#blocking]
98[heading Blocking]
99
100Normally, when this documentation states that a particular fiber ['blocks] (or
101equivalently, ['suspends),] it means that it yields control, allowing other
102fibers on the same thread to run. The synchronization mechanisms provided by
103__boost_fiber__ have this behavior.
104
105A fiber may, of course, use normal thread synchronization mechanisms; however
106a fiber that invokes any of these mechanisms will block its entire thread,
107preventing any other fiber from running on that thread in the meantime. For
108instance, when a fiber wants to wait for a value from another fiber in the
109same thread, using `std::future` would be unfortunate: `std::future::get()` would
110block the whole thread, preventing the other fiber from delivering its value.
111Use __future__ instead.
112
113Similarly, a fiber that invokes a normal blocking I/O operation will block its
114entire thread. Fiber authors are encouraged to consistently use asynchronous
115I/O. __boost_asio__ and other asynchronous I/O operations can
116straightforwardly be adapted for __boost_fiber__: see [link callbacks
117Integrating Fibers with Asynchronous Callbacks].
118
119__boost_fiber__ depends upon __boost_context__.
120Boost version 1.61.0 or greater is required.
121
122[note This library requires C++11!]
123
124
125[endsect]