1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Event-Driven Program</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Fiber"> 8<link rel="up" href="../integration.html" title="Sharing a Thread with Another Main Loop"> 9<link rel="prev" href="overview.html" title="Overview"> 10<link rel="next" href="embedded_main_loop.html" title="Embedded Main Loop"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="embedded_main_loop.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="fiber.integration.event_driven_program"></a><a class="link" href="event_driven_program.html" title="Event-Driven Program">Event-Driven 28 Program</a> 29</h3></div></div></div> 30<p> 31 Consider a classic event-driven program, organized around a main loop that 32 fetches and dispatches incoming I/O events. You are introducing <span class="bold"><strong>Boost.Fiber</strong></span> because certain asynchronous I/O sequences 33 are logically sequential, and for those you want to write and maintain code 34 that looks and acts sequential. 35 </p> 36<p> 37 You are launching fibers on the application’s main thread because certain 38 of their actions will affect its user interface, and the application’s UI 39 framework permits UI operations only on the main thread. Or perhaps those 40 fibers need access to main-thread data, and it would be too expensive in 41 runtime (or development time) to robustly defend every such data item with 42 thread synchronization primitives. 43 </p> 44<p> 45 You must ensure that the application’s main loop <span class="emphasis"><em>itself</em></span> 46 doesn’t monopolize the processor: that the fibers it launches will get the 47 CPU cycles they need. 48 </p> 49<p> 50 The solution is the same as for any fiber that might claim the CPU for an 51 extended time: introduce calls to <a class="link" href="../fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>. The 52 most straightforward approach is to call <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> on every iteration of your existing main 53 loop. In effect, this unifies the application’s main loop with <span class="bold"><strong>Boost.Fiber</strong></span>’s 54 internal main loop. <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> allows the fiber manager to run any fibers 55 that have become ready since the previous iteration of the application’s main 56 loop. When these fibers have had a turn, control passes to the thread’s main 57 fiber, which returns from <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> and resumes the application’s main loop. 58 </p> 59</div> 60<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 61<td align="left"></td> 62<td align="right"><div class="copyright-footer">Copyright © 2013 Oliver Kowalke<p> 63 Distributed under the Boost Software License, Version 1.0. (See accompanying 64 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 65 </p> 66</div></td> 67</tr></table> 68<hr> 69<div class="spirit-nav"> 70<a accesskey="p" href="overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="embedded_main_loop.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 71</div> 72</body> 73</html> 74