<!-- This file is generated from internal wiki guide by folly/facebook/fibers-update-readme.sh. -->
<section class="dex_guide"><h1 class="dex_title">folly::fibers</h1><section class="dex_document"><h1></h1><p class="dex_introduction">folly::fibers is an async C++ framework, which uses fibers for parallelism.</p><h2 id="overview">Overview <a href="#overview" class="headerLink">#</a></h2>
<p>Fibers (or coroutines) are lightweight application threads. Multiple fibers can be running on top of a single system thread. Unlike system threads, all the context switching between fibers is happening explicitly. Because of this every such context switch is very fast (~200 million of fiber context switches can be made per second on a single CPU core).</p>
<p>folly::fibers implements a task manager (FiberManager), which executes scheduled tasks on fibers. It also provides some fiber-compatible synchronization primitives.</p>
<h2 id="basic-example">Basic example <a href="#basic-example" class="headerLink">#</a></h2>
<div class="remarkup-code-block" data-code-lang="php"><pre class="remarkup-code"><span class="o">...</span>
<span class="nc" data-symbol-name="folly">folly</span><span class="o">::</span><span class="na" data-symbol-context="folly" data-symbol-name="EventBase">EventBase</span> <span class="no">evb</span><span class="o">;</span>
<span class="no">auto</span><span class="o">&</span> <span class="no">fiberManager</span> <span class="o">=</span> <span class="nc" data-symbol-name="folly">folly</span><span class="o">::</span><span class="na" data-symbol-context="folly" data-symbol-name="fibers">fibers</span><span class="o">::</span><span class="na" data-symbol-name="getFiberManager">getFiberManager</span><span class="o">(</span><span class="no">evb</span><span class="o">);</span>
<span class="nc" data-symbol-name="folly">folly</span><span class="o">::</span><span class="na" data-symbol-context="folly" data-symbol-name="fibers">fibers</span><span class="o">::</span><span class="na" data-symbol-name="Baton">Baton</span> <span class="no">baton</span><span class="o">;</span>
<span class="no">fiberManager</span><span class="o">.</span><span class="nf" data-symbol-name="addTask">addTask</span><span class="o">([&]()</span> <span class="o">{</span>
<span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="cout">cout</span> <span class="o"><<</span> <span class="s2">"Task 1: start"</span> <span class="o"><<</span> <span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="endl">endl</span><span class="o">;</span>
<span class="no">baton</span><span class="o">.</span><span class="nf" data-symbol-name="wait">wait</span><span class="o">();</span>
<span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="cout">cout</span> <span class="o"><<</span> <span class="s2">"Task 1: after baton.wait()"</span> <span class="o"><<</span> <span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="endl">endl</span><span class="o">;</span>
<span class="o">});</span>
<span class="no">fiberManager</span><span class="o">.</span><span class="nf" data-symbol-name="addTask">addTask</span><span class="o">([&]()</span> <span class="o">{</span>
<span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="cout">cout</span> <span class="o"><<</span> <span class="s2">"Task 2: start"</span> <span class="o"><<</span> <span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="endl">endl</span><span class="o">;</span>
<span class="no">baton</span><span class="o">.</span><span class="nf" data-symbol-name="post">post</span><span class="o">();</span>
<span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="cout">cout</span> <span class="o"><<</span> <span class="s2">"Task 2: after baton.post()"</span> <span class="o"><<</span> <span class="nc" data-symbol-name="std">std</span><span class="o">::</span><span class="na" data-symbol-context="std" data-symbol-name="endl">endl</span><span class="o">;</span>
<span class="o">});</span>
<span class="no">evb</span><span class="o">.</span><span class="nf" data-symbol-name="loop">loop</span><span class="o">();</span>
<span class="o">...</span></pre></div>
<p>This would print:</p>
<div class="remarkup-code-block" data-code-lang="php"><pre class="remarkup-code"><span class="no">Task</span> <span class="mi">1</span><span class="o">:</span> <span class="no">start</span>
<span class="no">Task</span> <span class="mi">2</span><span class="o">:</span> <span class="no">start</span>
<span class="no">Task</span> <span class="mi">2</span><span class="o">:</span> <span class="no">after</span> <span class="no">baton</span><span class="o">.</span><span class="nf" data-symbol-name="post">post</span><span class="o">()</span>
<span class="no">Task</span> <span class="mi">1</span><span class="o">:</span> <span class="no">after</span> <span class="no">baton</span><span class="o">.</span><span class="nf" data-symbol-name="wait">wait</span><span class="o">()</span></pre></div>
<p>It's very important to note that both tasks in this example were executed on the same system thread. Task 1 was suspended by <tt>baton.wait()</tt> call. Task 2 then started and called <tt>baton.post()</tt>, resuming Task 1.</p>
<h2 id="features">Features <a href="#features" class="headerLink">#</a></h2>
<ul>
<li>Fibers creation and scheduling is performed by FiberManager</li>
<li>Integration with any event-management system (e.g. EventBase)</li>
<li>Low-level synchronization primitives (Baton) as well as higher-level primitives built on top of them (await, collectN, mutexes, ... )</li>
<li>Synchronization primitives have timeout support</li>
<li>Built-in mechanisms for fiber stack-overflow detection</li>
<li>Optional fiber-local data (i.e. equivalent of thread locals)</li>
</ul>
<h2 id="non-features">Non-features <a href="#non-features" class="headerLink">#</a></h2>
<ul>
<li>Individual fibers scheduling can't be directly controlled by application</li>
<li>FiberManager is not thread-safe (we recommend to keep one FiberManager per thread). Application is responsible for managing its own threads and distributing load between them</li>
<li>We don't support automatic stack size adjustments. Each fiber has a stack of fixed size.</li>
</ul>
<h2 id="why-would-i-not-want-to">Why would I not want to use fibers ? <a href="#why-would-i-not-want-to" class="headerLink">#</a></h2>
<p>The only real downside to using fibers is the need to keep a pre-allocated stack for every fiber being run. That either makes your application use a lot of memory (if you have many concurrent tasks and each of them uses large stacks) or creates a risk of stack overflow bugs (if you try to reduce the stack size).</p>
<p>We believe these problems can be addressed (and we provide some tooling for that), as fibers library is used in many critical applications at Facebook (mcrouter, TAO, Service Router). However, it's important to be aware of the risks and be ready to deal with stack issues if you decide to use fibers library in your application.</p>
<h2 id="what-are-the-alternative">What are the alternatives ? <a href="#what-are-the-alternative" class="headerLink">#</a></h2>
<ul>
<li><a href="https://github.com/facebook/folly/blob/master/folly/futures/" target="_blank">Futures</a> library works great for asynchronous high-level application code. Yet code
没有合适的资源?快使用搜索试试~ 我知道了~
C++工具库:Facebook 的 C++ 工具库 包含一系列高性能的 C++ 组件库,方便且高效在 Facebook 内部被广
共2000个文件
cpp:977个
h:927个
md:46个
需积分: 1 0 下载量 125 浏览量
2024-03-05
11:08:04
上传
评论
收藏 5.35MB ZIP 举报
温馨提示
Facebook 的 C++ 工具库。包含一系列高性能的 C++ 组件库,方便且高效在 Facebook 内部被广泛应用。该项目不仅代码规范测试用例充足,而且源码中包含丰富的注释。同样功能的函数为什么别人写的性能好还健壮,这次终于可以一探究竟了。
资源推荐
资源详情
资源评论
收起资源包目录
C++工具库:Facebook 的 C++ 工具库 包含一系列高性能的 C++ 组件库,方便且高效在 Facebook 内部被广 (2000个子文件)
fb_py_win_main.c 4KB
openat2.c 926B
AsyncSocketTest2.cpp 360KB
farmhash_test.cpp 223KB
Blake2xbTest.cpp 170KB
AsyncSocket.cpp 144KB
AsyncSSLSocketTest.cpp 133KB
ObserverContainerTest.cpp 131KB
SharedMutexTest.cpp 98KB
FlatCombiningBenchmark.cpp 95KB
CollectTest.cpp 94KB
StlVectorTest.cpp 85KB
FibersTest.cpp 80KB
AsyncSSLSocket.cpp 78KB
F14MapTest.cpp 69KB
DistributedMutexTest.cpp 68KB
IoUringBackend.cpp 60KB
SmallLocksBenchmark.cpp 60KB
Compression.cpp 58KB
UnboundedQueueTest.cpp 57KB
farmhash.cpp 56KB
IOBufTest.cpp 55KB
IPAddressTest.cpp 54KB
AsyncIoUringSocket.cpp 52KB
RangeTest.cpp 52KB
CompressionTest.cpp 51KB
RelaxedConcurrentPriorityQueueTest.cpp 50KB
IOBufCursorTest.cpp 49KB
FBStringTest.cpp 49KB
heap_vector_types_test.cpp 48KB
DynamicTest.cpp 47KB
IoUringBackendTest.cpp 46KB
ConstexprMathTest.cpp 45KB
HazptrTest.cpp 45KB
AsyncUDPSocketTest.cpp 45KB
ConvTest.cpp 44KB
AsyncUDPSocket.cpp 44KB
IOBuf.cpp 44KB
StringTest.cpp 44KB
F14SetTest.cpp 43KB
CacheLocalityTest.cpp 43KB
FunctionTest.cpp 42KB
sorted_vector_test.cpp 42KB
FutureTest.cpp 41KB
TimeSeriesTest.cpp 41KB
small_vector_test.cpp 40KB
AsyncServerSocket.cpp 40KB
DynamicBoundedQueueTest.cpp 40KB
BaseTest.cpp 40KB
LockTest.cpp 38KB
SingletonTest.cpp 37KB
JsonTest.cpp 36KB
LtHashTest.cpp 36KB
SemiFutureTest.cpp 35KB
EventBase.cpp 34KB
ConcurrentHashMapTest.cpp 34KB
ObserverTest.cpp 34KB
Subprocess.cpp 34KB
TransformTest.cpp 33KB
MPMCQueueTest.cpp 33KB
ConvBenchmark.cpp 33KB
AtomicHashMapTest.cpp 33KB
TcpInfoTest.cpp 32KB
json.cpp 31KB
JSONSchema.cpp 31KB
ThreadPoolExecutorTest.cpp 31KB
IoUringBackendBench.cpp 31KB
SynchronizedTest.cpp 30KB
SocketAddressTest.cpp 30KB
HashTest.cpp 29KB
CollectTest.cpp 28KB
DwarfUtil.cpp 28KB
traits_test.cpp 27KB
AsyncIoUringSocketTest.cpp 27KB
Benchmark.cpp 27KB
ExpectedTest.cpp 27KB
SSLContext.cpp 27KB
SubprocessTest.cpp 26KB
SettingsTest.cpp 26KB
ExceptionWrapperTest.cpp 26KB
DwarfImpl.cpp 26KB
AsyncSSLSocketWriteTest.cpp 26KB
Conv.cpp 26KB
IteratorTest.cpp 25KB
SynchronizedBenchmark.cpp 25KB
FixedStringTest.cpp 24KB
NetOps.cpp 24KB
TraitsTest.cpp 24KB
FBVectorBenchmark.cpp 24KB
TaskTest.cpp 23KB
TryTest.cpp 23KB
CoroTest.cpp 23KB
AtomicUtilTest.cpp 23KB
SocketAddress.cpp 22KB
OptionalTest.cpp 22KB
PolyTest.cpp 22KB
String.cpp 22KB
WeightedEvictingCacheMapTest.cpp 21KB
RequestContextTest.cpp 21KB
MergeChannelTest.cpp 21KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
UnknownToKnown
- 粉丝: 1w+
- 资源: 773
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 鼎微R16中控升级包R16-4.5.10-20170221及强制升级方法
- 鼎微R16中控升级包公版UI 2015及强制升级方法,救砖包
- 基于CSS与JavaScript的积分系统设计源码
- 生物化学作业_1_生物化学作业资料.pdf
- 基于libgdx引擎的Java开发连连看游戏设计源码
- 基于MobileNetV3的SSD目标检测算法PyTorch实现设计源码
- 基于Java JDK的全面框架设计源码学习项目
- 基于Python黑魔法原理的Python编程技巧设计源码
- 基于Python的EducationCRM管理系统前端设计源码
- 基于Django4.0+Python3.10的在线学习系统Scss设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功