book: typos fix in ch07-thread (#118)

revise the code to make sure it consistent with the book description
This commit is contained in:
changdingfang
2020-08-28 23:24:05 +08:00
committed by GitHub
parent a3f9aaa394
commit 518c6e97d7
3 changed files with 12 additions and 9 deletions

View File

@@ -429,7 +429,7 @@ In order to achieve the ultimate performance and achieve consistency of various
std::atomic<int> counter = {0};
std::vector<std::thread> vt;
for (int i = 0; i < 100; ++i) {
vt.emplace_back([](){
vt.emplace_back([&](){
counter.fetch_add(1, std::memory_order_relaxed);
});
}
@@ -444,7 +444,8 @@ In order to achieve the ultimate performance and achieve consistency of various
2. Release/consumption model: In this model, we begin to limit the order of operations between processes. If a thread needs to modify a value, but another thread will have a dependency on that operation of the value, that is, the latter depends. former. Specifically, thread A has completed three writes to `x`, and thread `B` relies only on the third `x` write operation, regardless of the first two write behaviors of `x`, then `A ` When active `x.release()` (ie using `std::memory_order_release`), the option `std::memory_order_consume` ensures that `B` observes `A` when calling `x.load()` Three writes to `x`. Let's look at an example:
```cpp
std::atomic<int*> ptr;
// initialize as nullptr to prevent consumer load a dangling pointer
std::atomic<int*> ptr(nullptr);
int v;
std::thread producer([&]() {
int* p = new int(42);
@@ -500,7 +501,7 @@ In order to achieve the ultimate performance and achieve consistency of various
std::atomic<int> counter = {0};
std::vector<std::thread> vt;
for (int i = 0; i < 100; ++i) {
vt.emplace_back([](){
vt.emplace_back([&](){
counter.fetch_add(1, std::memory_order_seq_cst);
});
}

View File

@@ -439,7 +439,7 @@ int main() {
std::atomic<int> counter = {0};
std::vector<std::thread> vt;
for (int i = 0; i < 100; ++i) {
vt.emplace_back([](){
vt.emplace_back([&](){
counter.fetch_add(1, std::memory_order_relaxed);
});
}
@@ -453,7 +453,8 @@ int main() {
2. 释放/消费模型:在此模型中,我们开始限制进程间的操作顺序,如果某个线程需要修改某个值,但另一个线程会对该值的某次操作产生依赖,即后者依赖前者。具体而言,线程 A 完成了三次对 `x` 的写操作,线程 `B` 仅依赖其中第三次 `x` 的写操作,与 `x` 的前两次写行为无关,则当 `A` 主动 `x.release()` 时候(即使用 `std::memory_order_release`),选项 `std::memory_order_consume` 能够确保 `B` 在调用 `x.load()` 时候观察到 `A` 中第三次对 `x` 的写操作。我们来看一个例子:
```cpp
std::atomic<int*> ptr;
// 初始化为 nullptr 防止 consumer 线程从野指针进行读取
std::atomic<int*> ptr(nullptr);
int v;
std::thread producer([&]() {
int* p = new int(42);
@@ -509,7 +510,7 @@ int main() {
std::atomic<int> counter = {0};
std::vector<std::thread> vt;
for (int i = 0; i < 100; ++i) {
vt.emplace_back([](){
vt.emplace_back([&](){
counter.fetch_add(1, std::memory_order_seq_cst);
});
}