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

Commit 16109ab

Browse files
committed
修改2、3、4 章中 lock_guard 格式问题,确保类型与对象标识符之间存在空格
1 parent f3f03ce commit 16109ab

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

‎md/03共享数据.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ int main() {
138138

139139
```cpp
140140
void f() {
141-
std::lock_guard<std::mutex>lc{ m };
141+
std::lock_guard<std::mutex>lc{ m };
142142
std::cout << std::this_thread::get_id() << '\n';
143143
}
144144
```
@@ -531,12 +531,12 @@ void lock() { // lock the mutex
531531
std::mutex m;
532532

533533
int main() {
534-
std::unique_lock<std::mutex>lock{ m,std::adopt_lock };
534+
std::unique_lock<std::mutex>lock{ m,std::adopt_lock };
535535
lock.lock();
536536
}
537537
```
538538

539-
这段代码运行会[抛出异常](https://godbolt.org/z/KqKrETe6d),原因很简单,因为 `std::adopt_lock` 只是不上锁,但是**有所有权**,即 `_Owns` 设置为 `true` 了,当运行 `lock()` 成员函数的时候,调用了 `_Validate()` 进行检测,也就是:
539+
这段代码运行会[抛出异常](https://godbolt.org/z/r44nWeb16),原因很简单,因为 `std::adopt_lock` 只是不上锁,但是**有所有权**,即 `_Owns` 设置为 `true` 了,当运行 `lock()` 成员函数的时候,调用了 `_Validate()` 进行检测,也就是:
540540

541541
```cpp
542542
void _Validate() const { // check if the mutex can be locked
@@ -565,7 +565,7 @@ std::mutex m;
565565

566566
int main() {
567567
m.lock();
568-
std::unique_lock<std::mutex>lock { m,std::adopt_lock };
568+
std::unique_lock<std::mutex>lock { m,std::adopt_lock };
569569
}
570570
```
571571

‎md/04同步操作.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool flag = false;
3333
std::mutex m;
3434

3535
void wait_for_flag(){
36-
std::unique_lock<std::mutex>lk{ m };
36+
std::unique_lock<std::mutex>lk{ m };
3737
while (!flag){
3838
lk.unlock(); // 1 解锁互斥量
3939
lk.lock(); // 2 上锁互斥量
@@ -45,7 +45,7 @@ void wait_for_flag(){
4545

4646
```cpp
4747
void wait_for_flag(){
48-
std::unique_lock<std::mutex>lk{ m };
48+
std::unique_lock<std::mutex>lk{ m };
4949
while (!flag){
5050
lk.unlock(); // 1 解锁互斥量
5151
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 2 休眠
@@ -149,7 +149,7 @@ public:
149149
threadsafe_queue() {}
150150
void push(T new_value) {
151151
{
152-
std::lock_guard<std::mutex>lk { m };
152+
std::lock_guard<std::mutex>lk { m };
153153
data_queue.push(new_value);
154154
}
155155
data_cond.notify_one();
@@ -170,7 +170,7 @@ public:
170170
return res;
171171
}
172172
bool empty()const {
173-
std::lock_guard<std::mutex>lk (m);
173+
std::lock_guard<std::mutex>lk (m);
174174
return data_queue.empty();
175175
}
176176
};
@@ -201,7 +201,7 @@ std::cout << "push:" << new_value << std::endl;
201201
std::cout << "pop:" << value << std::endl;
202202
```
203203

204-
**可能**[**运行**](https://godbolt.org/z/77666WGP8)结果是:
204+
**可能**[**运行**](https://godbolt.org/z/Mh4bYGsTP)结果是:
205205

206206
```txt
207207
push:0

‎md/详细分析/02scoped_lock源码解析.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@
8181
```cpp
8282
std::mutex m1,m2;
8383
84-
std::scoped_lock<std::mutex>lc{ m1 }; // 匹配到偏特化版本 保有一个 std::mutex&
85-
std::scoped_lock<std::mutex, std::mutex>lc2{ m1,m2 }; // 匹配到主模板 保有一个 std::tuple<std::mutex&,std::mutex&>
84+
std::scoped_lock<std::mutex>lc{ m1 }; // 匹配到偏特化版本 保有一个 std::mutex&
85+
std::scoped_lock<std::mutex, std::mutex>lc2{ m1,m2 }; // 匹配到主模板 保有一个 std::tuple<std::mutex&,std::mutex&>
8686
std::scoped_lock<> lc3; // 匹配到全特化版本 空
8787
```
8888

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /