 When Lock-Free Still Isn't Enough: An Introduction to Wait-Free Programming and Concurrency Techniquesstd::atomic does and what it is used for • You’ve heard of lock-free programming and know what a compare_exchange is Daniel Anderson -- danielanderson.net4 Our motivating problem • Required by std::weak_ptr When Lock-Free Still Isn't Enough: An Introduction to Wait-Free Programming and Concurrency Techniquesstd::atomic does and what it is used for • You’ve heard of lock-free programming and know what a compare_exchange is Daniel Anderson -- danielanderson.net4 Our motivating problem • Required by std::weak_ptr- ::lock uint64_t read() { return counter.load(); } std::atomic - counter{1}; }; compare_exchange(expected&, desired) { if (current_value == expected) { current_value = desired; uint64_t read() { return counter.load(); } std::atomic - counter{1}; }; compare_exchange(expected&, desired) { if (current_value == expected) { current_value = desired; 0 码力 | 33 页 | 817.96 KB | 6 月前3
 Multi Producer, Multi Consumer, Lock Free, Atomic Queuec_queueC++ Atomic Operations / CPU instructions ● std::atomic Multi Producer, Multi Consumer, Lock Free, Atomic Queuec_queueC++ Atomic Operations / CPU instructions ● std::atomic- - provides load / store / compare_exchange ● std::atomic - ::is_always_lock_free ● Load and Store of aligned 8 bytes ● CAS ( old-expected data_entry{static_cast - ((wr_index << 1) | 1U), d}; if (_array[wr_index].compare_exchange(e, data_entry)) { // <=== DWCAS atomic instruction if constexpr (!lazy_push) empty_entry{static_cast - ((rd_index + _array.size()) << 1U)}; if (_array[rd_index].compare_exchange(e, empty_entry)) { // <=== DWCAS atomic instruction d = e.get_data(); 0 码力 | 54 页 | 886.12 KB | 6 月前3
 Lock-Free Atomic Shared Pointers Without a Split Reference Count? It Can Be Done!collection in C++ Some assumed knowledge • You’ve heard of lock-free programming and know what a compare_exchange is • You know roughly what atomic does and what it is used for • You’ve heard of shared_ptr collection in C++ Some assumed knowledge • You’ve heard of lock-free programming and know what a compare_exchange is • You know roughly what atomic does and what it is used for • You’ve heard of shared_ptr shared_ptr Lock-Free Atomic Shared Pointers Without a Split Reference Count? It Can Be Done!collection in C++ Some assumed knowledge • You’ve heard of lock-free programming and know what a compare_exchange is • You know roughly what atomic does and what it is used for • You’ve heard of shared_ptr collection in C++ Some assumed knowledge • You’ve heard of lock-free programming and know what a compare_exchange is • You know roughly what atomic does and what it is used for • You’ve heard of shared_ptr shared_ptr- load() • bool compare_exchange_weak(shared_ptr - & expected, shared_ptr - desired) • … compare_exchange(expected, desired) { if (current_value == expected) current_value = desired else expected 0 码力 | 45 页 | 5.12 MB | 6 月前3
 C++ Memory Model: from C++11 to C++23Is += a single/atomic instruction? How about ++? • How many reads/writes are needed for compare_exchange? What if it fails • foo(int volatile n) int volatile foo() are meaningless.Alex Dathskovsky0 码力 | 112 页 | 5.17 MB | 6 月前3 C++ Memory Model: from C++11 to C++23Is += a single/atomic instruction? How about ++? • How many reads/writes are needed for compare_exchange? What if it fails • foo(int volatile n) int volatile foo() are meaningless.Alex Dathskovsky0 码力 | 112 页 | 5.17 MB | 6 月前3
共 4 条
- 1













