Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit518c6e9

Browse files
book: typos fix in ch07-thread (changkun#118)
revise the code to make sure it consistent with the book description
1 parenta3f9aaa commit518c6e9

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

‎book/en-us/07-thread.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ In order to achieve the ultimate performance and achieve consistency of various
429429
std::atomic<int> counter = {0};
430430
std::vector<std::thread> vt;
431431
for (int i = 0; i < 100; ++i) {
432-
vt.emplace_back([](){
432+
vt.emplace_back([&](){
433433
counter.fetch_add(1, std::memory_order_relaxed);
434434
});
435435
}
@@ -444,7 +444,8 @@ In order to achieve the ultimate performance and achieve consistency of various
444444
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:
445445
446446
```cpp
447-
std::atomic<int*> ptr;
447+
// initialize as nullptr to prevent consumer load a dangling pointer
448+
std::atomic<int*> ptr(nullptr);
448449
int v;
449450
std::thread producer([&]() {
450451
int* p = new int(42);
@@ -500,7 +501,7 @@ In order to achieve the ultimate performance and achieve consistency of various
500501
std::atomic<int> counter = {0};
501502
std::vector<std::thread> vt;
502503
for (int i = 0; i < 100; ++i) {
503-
vt.emplace_back([](){
504+
vt.emplace_back([&](){
504505
counter.fetch_add(1, std::memory_order_seq_cst);
505506
});
506507
}

‎book/zh-cn/07-thread.md

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

‎code/7/7.8.memory.order.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
usingnamespacestd;
1616
usingnamespacestd::chrono;
1717

18-
atomic<int> counter = {0};
1918
constint N =10000;
2019

2120
voidrelaxed_order() {
2221
cout <<"relaxed_order:" << endl;
2322

23+
atomic<int> counter = {0};
2424
vector<thread> vt;
2525
for (int i =0; i < N; ++i) {
26-
vt.emplace_back([](){
26+
vt.emplace_back([&](){
2727
counter.fetch_add(1, memory_order_relaxed);
2828
});
2929
}
@@ -86,9 +86,10 @@ void release_acquire_order() {
8686
voidsequential_consistent_order() {
8787
cout <<"sequential_consistent_order:" << endl;
8888

89+
atomic<int> counter = {0};
8990
vector<thread> vt;
9091
for (int i =0; i < N; ++i) {
91-
vt.emplace_back([](){
92+
vt.emplace_back([&](){
9293
counter.fetch_add(1, memory_order_seq_cst);
9394
});
9495
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp