Home | History | Annotate | Download | only in util.smartptr.shared.atomic
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <memory>
     11 
     12 // shared_ptr
     13 
     14 // template <class T>
     15 // bool
     16 // atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
     17 //                                       shared_ptr<T> w, memory_order success,
     18 //                                       memory_order failure);
     19 
     20 #include <memory>
     21 #include <cassert>
     22 
     23 int main()
     24 {
     25 #if __has_feature(cxx_atomic)
     26     {
     27         std::shared_ptr<int> p(new int(4));
     28         std::shared_ptr<int> v(new int(3));
     29         std::shared_ptr<int> w(new int(2));
     30         bool b = std::atomic_compare_exchange_weak_explicit(&p, &v, w,
     31                                                             std::memory_order_seq_cst,
     32                                                             std::memory_order_seq_cst);
     33         assert(b == false);
     34         assert(*p == 4);
     35         assert(*v == 4);
     36         assert(*w == 2);
     37     }
     38     {
     39         std::shared_ptr<int> p(new int(4));
     40         std::shared_ptr<int> v = p;
     41         std::shared_ptr<int> w(new int(2));
     42         bool b = std::atomic_compare_exchange_weak_explicit(&p, &v, w,
     43                                                             std::memory_order_seq_cst,
     44                                                             std::memory_order_seq_cst);
     45         assert(b == true);
     46         assert(*p == 2);
     47         assert(*v == 4);
     48         assert(*w == 2);
     49     }
     50 #endif
     51 }
     52