元数据
C++ Concurrency in Action, Second Edition
- 书名: C++ Concurrency in Action, Second Edition
- 作者: Anthony Williams
- 简介:
- 出版时间:
- ISBN:
- 分类:
- 出版社: Manning Publications
- PC地址:https://weread.qq.com/web/reader/7db423f2a43425f31487933676333674736324e365a50365945cdf
高亮划线
Chapter 5. The C++ memory model and operations on atomic types
📌 The atomic types and operations allow just that,providing facilities for low-level synchronization operations that will commonly reduceto one or two CPU instructions. ⏱ 2022-06-16 22:07:41
📌 Whatever its type, an object is stored in one or more memory locations ⏱ 2022-06-16 22:11:22
📌 though adjacent bit fields are distinct objects, they’re still counted as thesame memory location. ⏱ 2022-06-16 23:00:39
📌 Every variable is an object, including those that are members of other objects.Every object occupies at least one memory location.Variables of fundamental types such as int or char occupy exactly one memory location,whatever their size, even if they’re adjacent or part of an array.Adjacent bit fields are part of the same memory location. ⏱ 2022-06-16 23:14:55
📌 you can also avoid the undefinedbehavior by using atomic operations to access the memory location involved in the race. ⏱ 2022-06-16 23:53:47
📌 This doesn’t prevent the race itself—which of the atomic operations touches the memorylocation first is still not specified—but it does bring the program back into the realm ofdefined behavior. ⏱ 2022-06-16 23:54:08
📌 modification orders. ⏱ 2022-06-16 23:54:19
📌 If different threads see distinct sequences of values for a single variable, youhave a data race and undefined behavior ⏱ 2022-06-16 23:57:43
📌 An atomic operation is an indivisible operation ⏱ 2022-06-17 00:02:13
📌 they(almost) all have an is_lock_free() member function, which allows the user to determinewhether operations on a given type are done directly with atomic instructions(x.is_lock_free() returns true) or done by using a lock internal to the compiler andlibrary (x.is_lock_free() returns false). ⏱ 2022-06-17 00:07:58
📌 the key use case for atomic operations is as areplacement for an operation that would otherwise use a mutex for synchronization ⏱ 2022-06-17 00:09:15
📌 SinceC++17, all atomic types have a static constexpr member variable,X::is_always_lock_free, which is true if and only if the atomic type X is lock-free for allsupported hardware that the output of the current compilation might run on. ⏱ 2022-06-17 00:10:13
📌 heyspecify the lock-free status of the corresponding atomic types for the specified built-intypes and their unsigned counterparts ⏱ 2022-06-17 00:12:24
📌 They evaluate to the value 0 if the atomic type is never lock-free, tothe value 2 if the atomic type is always lock-free, and to the value 1 if the lock-freestatus of the corresponding atomic type is a runtime property as described previously. ⏱ 2022-06-17 00:15:01
📌 std::atomic_flag. This type is a simple Boolean flag, and operations on this type arerequired to be lock-free ⏱ 2022-06-17 00:15:18
📌 objects of the std::atomic_flag type are initializedto clear, and they can then either be queried and set (with the test_and_set() memberfunction) or cleared (with the clear() member function). That’s it: no assignment, nocopy construction, no test and clear, no other operations at all. ⏱ 2022-06-17 00:18:16
📌 It’s generally simpler to say std::atomic
for whichever T youwant to work with, rather than use the alternative names. ⏱ 2022-06-17 00:21:40 ^CB-1Hy3gc3gG62N6ZP6YE-15-32881-33009
📌 The standard atomic types are not copyable or assignable in the conventional sense, inthat they have no copy constructors or copy assignment operators. ⏱ 2022-06-17 00:21:50
📌 They also support thecompound assignment operators where appropriate: +=, -=, *=, |= ⏱ 2022-06-17 00:22:22
📌 The return value from the assignment operators andmember functions is either the value stored (in the case of the assignment operators) orthe value prior to the operation (in the case of the named functions). ⏱ 2022-06-17 00:24:06
📌 Because it’s a generic class template, the operations are limited to load(), store() (and assignment from and conversion to the user-defined type), exchange(), compare_exchange_weak(), and compare_exchange_strong(). ⏱ 2022-06-17 10:30:39
📌 std::memory_order_relaxed, std::memory_order_acquire, std::memory_order_consume, std::memory_order_acq_rel,std::memory_order_release, and std::memory_order_seq_cst. ⏱ 2022-06-17 00:26:12
📌 The permitted values for the memory ordering depend on the operation category. ⏱ 2022-06-17 00:26:24
📌 the default ordering is used, which is the strongestordering: std::memory_order_seq_cst. ⏱ 2022-06-17 00:26:44
📌 Store operations, which can have memory_order_relaxed, memory_order_release, or memory_order_seq_cst orderingLoad operations, which can have memory_order_relaxed, memory_order_consume, memory_order_acquire, or memory_order_seq_cst orderingRead-modify-write operations, which can have memory_order_relaxed, memory_order_consume, memory_order_acquire, memory_order_release, memory_order_acq_rel, or memory_order_seq_cst ordering ⏱ 2022-06-17 00:27:40
📌 Objects of the std::atomic_flag type must be initialized with ATOMIC_FLAG_INIT. ⏱ 2022-06-17 10:32:22
📌 the only typeguaranteed to be lock-free ⏱ 2022-06-18 23:02:29
📌 there are only three things you can do with it:destroy it, clear it, or set it and query the previous value. These correspond to thedestructor, the clear() member function, and the test_and_set() member function,respectively. ⏱ 2022-06-18 23:03:11
📌 As with every atomicoperation, the default for both is memory_order_seq_cst ⏱ 2022-06-18 23:03:40
📌 All operations on an atomic type aredefined as atomic, and assignment and copy-construction involve two objects. ⏱ 2022-06-18 23:04:29
📌 The limited feature set makes std::atomic_flag ideally suited to use as a spin-lock mutex. ⏱ 2022-06-18 23:09:58
📌 One other thing to note about the assignment operator from a non-atomic bool is that itdiffers from the general convention of returning a reference to the object it’s assigned to:it returns a bool with the value assigned instead. ⏱ 2022-06-18 23:12:22
📌 another common pattern withthe atomic types: the assignment operators they support return values (of thecorresponding non-atomic type) rather than references. ⏱ 2022-06-18 23:12:32
📌 clear() function of std::atomic_flag, writes (of eithertrue or false) are done by calling store(), although the memory-order semantics can stillbe specified. Similarly, test_and_set() has been replaced with the more generalexchange() member function that allows you to replace the stored value with a new oneof your choosing and atomically retrieve the original value. ⏱ 2022-06-18 23:14:23
📌 As you might expect, store() is a store operation,whereas load() is a load operation. exchange() is a read-modify-write operation ⏱ 2022-06-18 23:15:59
📌 exchange() isn’t the only read-modify-write operation supported by std::atomic
;it also introduces an operation to store a new value if the current value is equal to anexpected value. ⏱ 2022-06-18 23:16:25 ^CB-1Hy3gc3gG62N6ZP6YE-15-48403-48599
📌 This new operation is called compare-exchange, and it comes in the form of thecompare_exchange_weak() and compare_exchange_strong() member functions. ⏱ 2022-06-18 23:16:52
📌 The return type of the compare-exchangefunctions is a bool, which is true if the store was performed and false otherwise. ⏱ 2022-06-18 23:17:57
📌 For compare_exchange_weak(), the store might not be successful even if the originalvalue was equal to the expected value, in which case the value of the variable isunchanged and the return value of compare_exchange_weak() is false. ⏱ 2022-06-18 23:18:40
📌 This is called a spurious failure,because the reason for the failure is a function of timing rather than the values of thevariables. ⏱ 2022-06-18 23:20:12
📌 On the other hand, compare_exchange_strong() is guaranteed to return false only if thevalue wasn’t equal to the expected value. ⏱ 2022-06-18 23:21:52
读书笔记
Chapter 5. The C++ memory model and operations on atomic types
划线评论
📌 race condition ^15826765-7A47fWo2m - 💭 Race Condition中文翻译是竞争条件,是指多个进程或者线程并发访问和操作同一数据且执行结果与访问发生的特定顺序有关的现象。 - ⏱ 2022-06-16 23:18:34