1//// 2Copyright 2017 Peter Dimov 3 4Distributed under the Boost Software License, Version 1.0. 5 6See accompanying file LICENSE_1_0.txt or copy at 7http://www.boost.org/LICENSE_1_0.txt 8//// 9 10[#atomic_shared_ptr] 11# atomic_shared_ptr 12:toc: 13:toc-title: 14:idprefix: atomic_shared_ptr_ 15 16## Description 17 18The class template `atomic_shared_ptr<T>` implements the interface of `std::atomic` 19for a contained value of type `shared_ptr<T>`. Concurrent access to `atomic_shared_ptr` 20is not a data race. 21 22## Synopsis 23 24`atomic_shared_ptr` is defined in `<boost/smart_ptr/atomic_shared_ptr.hpp>`. 25 26``` 27namespace boost { 28 29 template<class T> class atomic_shared_ptr { 30 private: 31 32 shared_ptr<T> p_; // exposition only 33 34 atomic_shared_ptr(const atomic_shared_ptr&) = delete; 35 atomic_shared_ptr& operator=(const atomic_shared_ptr&) = delete; 36 37 public: 38 39 constexpr atomic_shared_ptr() noexcept; 40 atomic_shared_ptr( shared_ptr<T> p ) noexcept; 41 42 atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept; 43 44 bool is_lock_free() const noexcept; 45 46 shared_ptr<T> load( int = 0 ) const noexcept; 47 operator shared_ptr<T>() const noexcept; 48 49 void store( shared_ptr<T> r, int = 0 ) noexcept; 50 51 shared_ptr<T> exchange( shared_ptr<T> r, int = 0 ) noexcept; 52 53 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept; 54 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept; 55 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept; 56 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept; 57 58 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept; 59 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept; 60 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept; 61 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept; 62 }; 63} 64``` 65 66## Members 67 68``` 69constexpr atomic_shared_ptr() noexcept; 70``` 71[none] 72* {blank} 73+ 74Effects:: Default-initializes `p_`. 75 76``` 77atomic_shared_ptr( shared_ptr<T> p ) noexcept; 78``` 79[none] 80* {blank} 81+ 82Effects:: Initializes `p_` to `p`. 83 84``` 85atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept; 86``` 87[none] 88* {blank} 89+ 90Effects:: `p_.swap(r)`. 91Returns:: `*this`. 92 93``` 94bool is_lock_free() const noexcept; 95``` 96[none] 97* {blank} 98+ 99Returns:: `false`. 100 101NOTE: This implementation is not lock-free. 102 103``` 104shared_ptr<T> load( int = 0 ) const noexcept; 105``` 106``` 107operator shared_ptr<T>() const noexcept; 108``` 109[none] 110* {blank} 111+ 112Returns:: `p_`. 113 114NOTE: The `int` argument is intended to be of type `memory_order`, but is ignored. 115 This implementation is lock-based and therefore always sequentially consistent. 116 117``` 118void store( shared_ptr<T> r, int = 0 ) noexcept; 119``` 120[none] 121* {blank} 122+ 123Effects:: `p_.swap(r)`. 124 125``` 126shared_ptr<T> exchange( shared_ptr<T> r, int = 0 ) noexcept; 127``` 128[none] 129* {blank} 130+ 131Effects:: `p_.swap(r)`. 132Returns:: The old value of `p_`. 133 134``` 135bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept; 136``` 137``` 138bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept; 139``` 140``` 141bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept; 142``` 143``` 144bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept; 145``` 146[none] 147* {blank} 148+ 149Effects:: If `p_` is equivalent to `v`, assigns `w` to `p_`, otherwise assigns `p_` to `v`. 150Returns:: `true` if `p_` was equivalent to `v`, `false` otherwise. 151Remarks:: Two `shared_ptr` instances are equivalent if they store the same pointer value and _share ownership_. 152 153``` 154bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept; 155``` 156``` 157bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept; 158``` 159``` 160bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept; 161``` 162``` 163bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept; 164``` 165[none] 166* {blank} 167+ 168Effects:: If `p_` is equivalent to `v`, assigns `std::move(w)` to `p_`, otherwise assigns `p_` to `v`. 169Returns:: `true` if `p_` was equivalent to `v`, `false` otherwise. 170Remarks:: The old value of `w` is not preserved in either case. 171