1 #include <atomic> 2 3 template <class A, class T> 4 bool cmpxchg_weak_loop(A& atomic, T& expected, T desired) { 5 for (int i = 0; i < 10; i++) { 6 if (atomic.compare_exchange_weak(expected, desired) == true) { 7 return true; 8 } 9 } 10 11 return false; 12 } 13 14 template <class A, class T> 15 bool cmpxchg_weak_loop(A& atomic, T& expected, T desired, 16 std::memory_order success, 17 std::memory_order failure) { 18 for (int i = 0; i < 10; i++) { 19 if (atomic.compare_exchange_weak(expected, desired, success, 20 failure) == true) { 21 return true; 22 } 23 } 24 25 return false; 26 } 27 28 template <class A, class T> 29 bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired) { 30 for (int i = 0; i < 10; i++) { 31 if (std::atomic_compare_exchange_weak(atomic, expected, desired) == true) { 32 return true; 33 } 34 } 35 36 return false; 37 } 38 39 template <class A, class T> 40 bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired, 41 std::memory_order success, 42 std::memory_order failure) { 43 for (int i = 0; i < 10; i++) { 44 if (std::atomic_compare_exchange_weak_explicit(atomic, expected, desired, 45 success, failure) == true) { 46 return true; 47 } 48 } 49 50 return false; 51 } 52