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 13similiarly to __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 __cc__ from __boost_context__; the classes 25in this library manage, schedule and, when needed, synchronize those execution 26contexts. 27A context switch between threads usually costs thousands of CPU cycles on x86, 28compared to a fiber switch with less than a hundred cycles. 29A fiber runs on a single thread at any point in time. 30 31In order to use the classes and functions described here, you can either include 32the specific headers specified by the descriptions of each class or function, or 33include the master library header: 34 35 #include <boost/fiber/all.hpp> 36 37which includes all the other headers in turn. 38 39The namespaces used are: 40 41 namespace boost::fibers 42 namespace boost::this_fiber 43 44[heading Fibers and Threads] 45 46Control is cooperatively passed between fibers launched on a given thread. At 47a given moment, on a given thread, at most one fiber is running. 48 49Spawning additional fibers on a given thread does not distribute your program 50across more hardware cores, though it can make more effective use of the core 51on which it's running. 52 53On the other hand, a fiber may safely access any resource exclusively owned by 54its parent thread without explicitly needing to defend that resource against 55concurrent access by other fibers on the same thread. You are already 56guaranteed that no other fiber on that thread is concurrently touching that 57resource. This can be particularly important when introducing concurrency in 58legacy code. You can safely spawn fibers running old code, using asynchronous 59I/O to interleave execution. 60 61In effect, fibers provide a natural way to organize concurrent code based on 62asynchronous I/O. Instead of chaining together completion handlers, code 63running on a fiber can make what looks like a normal blocking function call. 64That call can cheaply suspend the calling fiber, allowing other fibers on the 65same thread to run. When the operation has completed, the suspended fiber 66resumes, without having to explicitly save or restore its state. Its local 67stack variables persist across the call. 68 69A fiber can be migrated from one thread to another, though the library does 70not do this by default. It is possible for you to supply a custom scheduler 71that migrates fibers between threads. You may specify custom fiber properties 72to help your scheduler decide which fibers are permitted to migrate. Please 73see [link migration Migrating fibers between threads] and [link custom 74Customization] for more details. 75 76__boost_fiber__ allows to [*`multiplex fibers across multiple cores`] (see 77__numa_work_stealing__). 78 79A fiber launched on a particular thread continues running on that thread 80unless migrated. It might be unblocked (see [link blocking Blocking] below) by 81some other thread, but that only transitions the fiber from ["blocked] to 82["ready] on its current thread [mdash] it does not cause the fiber to 83resume on the thread that unblocked it. 84 85[#thread_local_storage] 86[heading thread-local storage] 87Unless migrated, a fiber may access thread-local storage; however that storage 88will be shared among all fibers running on the same thread. For fiber-local 89storage, please see __fsp__. 90 91[#cross_thread_sync] 92[heading BOOST_FIBERS_NO_ATOMICS] 93The fiber synchronization objects provided by this library will, by default, 94safely synchronize fibers running on different threads. However, this level of 95synchronization can be removed (for performance) by building the library with 96[*`BOOST_FIBERS_NO_ATOMICS`] defined. When the library is built with that macro, 97you must ensure that all the fibers referencing a particular synchronization 98object are running in the same thread. Please see [link synchronization 99Synchronization]. 100 101[#blocking] 102[heading Blocking] 103 104Normally, when this documentation states that a particular fiber ['blocks] (or 105equivalently, ['suspends),] it means that it yields control, allowing other 106fibers on the same thread to run. The synchronization mechanisms provided by 107__boost_fiber__ have this behavior. 108 109A fiber may, of course, use normal thread synchronization mechanisms; however 110a fiber that invokes any of these mechanisms will block its entire thread, 111preventing any other fiber from running on that thread in the meantime. For 112instance, when a fiber wants to wait for a value from another fiber in the 113same thread, using `std::future` would be unfortunate: `std::future::get()` would 114block the whole thread, preventing the other fiber from delivering its value. 115Use __future__ instead. 116 117Similarly, a fiber that invokes a normal blocking I/O operation will block its 118entire thread. Fiber authors are encouraged to consistently use asynchronous 119I/O. __boost_asio__ and other asynchronous I/O operations can 120straightforwardly be adapted for __boost_fiber__: see [link callbacks 121Integrating Fibers with Asynchronous Callbacks]. 122 123__boost_fiber__ depends upon __boost_context__. 124Boost version 1.61.0 or greater is required. 125 126[note This library requires C++11!] 127 128 129[#implementation] 130[section Implementations: fcontext_t, ucontext_t and WinFiber] 131 132__boost_fiber__ uses __cc__ from __boost_context__ as building-block. 133 134[heading fcontext_t] 135 136The implementation uses __fcontext__ per default. fcontext_t is based on 137assembler and not available for all platforms. It provides a much better 138performance than __ucontext__ 139(the context switch takes two magnitudes of order less CPU cycles; see section 140[@http://www.boost.org/doc/libs/release/libs/context/doc/html/context/performance.html ['performance]]) 141and __winfib__. 142 143 144[heading ucontext_t] 145 146As an alternative, [@https://en.wikipedia.org/wiki/Setcontext __ucontext__] 147can be used by compiling with `BOOST_USE_UCONTEXT` and b2 property `context-impl=ucontext`. 148__ucontext__ might be available on a broader range of POSIX-platforms but has 149some [@http://www.boost.org/doc/libs/release/libs/context/doc/html/context/rational.html#ucontext ['disadvantages]] 150(for instance deprecated since POSIX.1-2003, not C99 conform). 151 152[note __cc__ supports [link segmented ['Segmented stacks]] only with 153__ucontext__ as its implementation.] 154 155 156[heading WinFiber] 157 158With `BOOST_USE_WINFIB` and b2 property `context-impl=winfib` Win32-Fibers are used 159as implementation for __cc__. 160 161Because the TIB (thread information block) is not fully described in the MSDN, 162it might be possible that not all required TIB-parts are swapped. 163 164[note The first call of __cc__ converts the thread into a Windows fiber by 165invoking `ConvertThreadToFiber()`. If desired, `ConvertFiberToThread()` has 166to be called by the user explicitly in order to release resources allocated 167by `ConvertThreadToFiber()` (e.g. after using boost.context). ] 168 169[endsect] 170 171[important Windows using fcontext_t: turn off global program optimization (/GL) and change /EHsc (compiler 172assumes that functions declared as extern "C" never throw a C++ exception) to /EHs (tells 173compiler assumes that functions declared as extern "C" may throw an exception).] 174 175 176[endsect] 177