1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Rationale</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../lockfree.html" title="Chapter 22. Boost.Lockfree"> 10<link rel="prev" href="examples.html" title="Examples"> 11<link rel="next" href="reference.html" title="Reference"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="examples.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../lockfree.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="lockfree.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a> 29</h2></div></div></div> 30<div class="toc"><dl class="toc"> 31<dt><span class="section"><a href="rationale.html#lockfree.rationale.data_structures">Data Structures</a></span></dt> 32<dt><span class="section"><a href="rationale.html#lockfree.rationale.memory_management">Memory Management</a></span></dt> 33<dt><span class="section"><a href="rationale.html#lockfree.rationale.aba_prevention">ABA Prevention</a></span></dt> 34<dt><span class="section"><a href="rationale.html#lockfree.rationale.interprocess_support">Interprocess 35 Support</a></span></dt> 36</dl></div> 37<div class="section"> 38<div class="titlepage"><div><div><h3 class="title"> 39<a name="lockfree.rationale.data_structures"></a><a class="link" href="rationale.html#lockfree.rationale.data_structures" title="Data Structures">Data Structures</a> 40</h3></div></div></div> 41<p> 42 The implementations are implementations of well-known data structures. The 43 queue is based on <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.37.3574" target="_top">Simple, 44 Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms 45 by Michael Scott and Maged Michael</a>, the stack is based on <a href="http://books.google.com/books?id=YQg3HAAACAAJ" target="_top">Systems programming: 46 coping with parallelism by R. K. Treiber</a> and the spsc_queue is considered 47 as 'folklore' and is implemented in several open-source projects including 48 the linux kernel. All data structures are discussed in detail in <a href="http://books.google.com/books?id=pFSwuqtJgxYC" target="_top">"The 49 Art of Multiprocessor Programming" by Herlihy & Shavit</a>. 50 </p> 51</div> 52<div class="section"> 53<div class="titlepage"><div><div><h3 class="title"> 54<a name="lockfree.rationale.memory_management"></a><a class="link" href="rationale.html#lockfree.rationale.memory_management" title="Memory Management">Memory Management</a> 55</h3></div></div></div> 56<p> 57 The lock-free <code class="computeroutput"><a class="link" href="../boost/lockfree/queue.html" title="Class template queue">boost::lockfree::queue</a></code> 58 and <code class="computeroutput"><a class="link" href="../boost/lockfree/stack.html" title="Class template stack">boost::lockfree::stack</a></code> 59 classes are node-based data structures, based on a linked list. Memory management 60 of lock-free data structures is a non-trivial problem, because we need to 61 avoid that one thread frees an internal node, while another thread still 62 uses it. <code class="literal">boost.lockfree</code> uses a simple approach not returning 63 any memory to the operating system. Instead they maintain a <span class="bold"><strong>free-list</strong></span> 64 in order to reuse them later. This is done for two reasons: first, depending 65 on the implementation of the memory allocator freeing the memory may block 66 (so the implementation would not be lock-free anymore), and second, most 67 memory reclamation algorithms are patented. 68 </p> 69</div> 70<div class="section"> 71<div class="titlepage"><div><div><h3 class="title"> 72<a name="lockfree.rationale.aba_prevention"></a><a class="link" href="rationale.html#lockfree.rationale.aba_prevention" title="ABA Prevention">ABA Prevention</a> 73</h3></div></div></div> 74<p> 75 The ABA problem is a common problem when implementing lock-free data structures. 76 The problem occurs when updating an atomic variable using a <code class="literal">compare_exchange</code> 77 operation: if the value A was read, thread 1 changes it to say C and tries 78 to update the variable, it uses <code class="literal">compare_exchange</code> to write 79 C, only if the current value is A. This might be a problem if in the meanwhile 80 thread 2 changes the value from A to B and back to A, because thread 1 does 81 not observe the change of the state. The common way to avoid the ABA problem 82 is to associate a version counter with the value and change both atomically. 83 </p> 84<p> 85 <code class="literal">boost.lockfree</code> uses a <code class="literal">tagged_ptr</code> helper 86 class which associates a pointer with an integer tag. This usually requires 87 a double-width <code class="literal">compare_exchange</code>, which is not available 88 on all platforms. IA32 did not provide the <code class="literal">cmpxchg8b</code> opcode 89 before the pentium processor and it is also lacking on many RISC architectures 90 like PPC. Early X86-64 processors also did not provide a <code class="literal">cmpxchg16b</code> 91 instruction. On 64bit platforms one can work around this issue, because often 92 not the full 64bit address space is used. On X86_64 for example, only 48bit 93 are used for the address, so we can use the remaining 16bit for the ABA prevention 94 tag. For details please consult the implementation of the <code class="literal">boost::lockfree::detail::tagged_ptr</code> 95 class. 96 </p> 97<p> 98 For lock-free operations on 32bit platforms without double-width <code class="literal">compare_exchange</code>, 99 we support a third approach: by using a fixed-sized array to store the internal 100 nodes we can avoid the use of 32bit pointers, but instead 16bit indices into 101 the array are sufficient. However this is only possible for fixed-sized data 102 structures, that have an upper bound of internal nodes. 103 </p> 104</div> 105<div class="section"> 106<div class="titlepage"><div><div><h3 class="title"> 107<a name="lockfree.rationale.interprocess_support"></a><a class="link" href="rationale.html#lockfree.rationale.interprocess_support" title="Interprocess Support">Interprocess 108 Support</a> 109</h3></div></div></div> 110<p> 111 The <code class="literal">boost.lockfree</code> data structures have basic support 112 for <a href="../../../libs/interprocess/index.html" target="_top">Boost.Interprocess</a>. 113 The only problem is the blocking emulation of lock-free atomics, which in 114 the current implementation is not guaranteed to be interprocess-safe. 115 </p> 116</div> 117</div> 118<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 119<td align="left"></td> 120<td align="right"><div class="copyright-footer">Copyright © 2008-2011 Tim 121 Blechmann<p> 122 Distributed under the Boost Software License, Version 1.0. (See accompanying 123 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>) 124 </p> 125</div></td> 126</tr></table> 127<hr> 128<div class="spirit-nav"> 129<a accesskey="p" href="examples.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../lockfree.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 130</div> 131</body> 132</html> 133