Skip to content

Commit f141ee0

Browse files
committed
add note
1 parent 7f25b54 commit f141ee0

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

_code/CppCon/2017/cpp_atomics.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,38 @@ Increment and decrement for raw pointers.
5252

5353
Compare and swap(CAS) is used in most lock free algorithms.
5454

55-
`fetch_add`...add and return old value.
55+
`fetch_add`... add and return old value.
56+
57+
`std::atomic` provides operator overloads only for atomic operations.
58+
59+
## Speed
60+
61+
Always measured performance
62+
63+
Comparing atomic operation with another thread-safe alternative is valid and useful.
64+
65+
ref: spinlock
66+
67+
`std::atomic` isn't always lock free.
68+
69+
`std::atomic::is_always_lock_free()` to check is the type lock free. It's runtime and platform dependent, based on alignment and padding.
70+
71+
Two thread access 2 variable with in 64 bytes will wait as if it was the same variable.
72+
73+
Atomic operations do wait on each other, write do, read only can scale near perfectly.
74+
75+
Atomic operations have to wait for cache line access.
76+
77+
Avoid false sharing by aligning per-thread data to separate cache lines.
78+
79+
## Strong and Weak compare-and-swap
80+
81+
c++ provides two versions of CAS
82+
83+
`compare_exchange_strong` and `compare_exchange_weak`
84+
85+
problem: if weak CAS correctly returns x == old+x, why would it fail?
86+
87+
Lock is not a real mutex but some form of exclusive access implemented in hardware.
88+
89+
Read is faster than write.

0 commit comments

Comments
 (0)