Skip to content

错字修改 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions md/03共享数据.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main() {
}
```

比如这段代码就是典型的恶性条件竞争,两个线程共享一个 `vector`,并对它进行修改。可能导致许多问题,比如 `f` 先执行,此时 `vector` 还没有元素,导致抛出异常。又或者 `f` 执行了一半,调用了 `f2()`,等等。
比如这段代码就是典型的恶性条件竞争,两个线程共享一个 `vector`,并对它进行修改。可能导致许多问题,比如 `f2` 先执行,此时 `vector` 还没有元素,导致抛出异常。又或者 `f` 执行了一半,调用了 `f2()`,等等。

当然了,也有可能先执行 f,然后执行 f2,最后打印了 0,程序老老实实执行完毕。

Expand Down Expand Up @@ -110,7 +110,7 @@ int main() {
}
```

当多个线程执行函数 `f` 的时候,只有一个线程能成功调用 `lock()` 给互斥量上锁,其他所有的线程 `lock()` 的调用将阻塞执行,直至获得锁。第一个调用 `lock()` 的线程得以继续往下执行,执行我们的 `std::cout` 输出语句,不会有任何的线程打断这个操作。直到线程执行 `unlock()`,就解锁了互斥体。
当多个线程执行函数 `f` 的时候,只有一个线程能成功调用 `lock()` 给互斥量上锁,其他所有的线程 `lock()` 的调用将阻塞执行,直至获得锁。第一个调用 `lock()` 的线程得以继续往下执行,执行我们的 `std::cout` 输出语句,不会有任何其他的线程打断这个操作。直到线程执行 `unlock()`,就解锁了互斥体。

那么其他线程此时也就能再有一个成功调用 `lock`...

Expand Down Expand Up @@ -245,7 +245,7 @@ std::mutex m;
std::scoped_lock lc{ m }; // std::scoped_lock<std::mutex>
```

- [**`std::scope_lock` 的源码实现与解析**](md/详细分析/02scoped_guard源码解析.md)
- [**`std::scoped_lock` 的源码实现与解析**](md/详细分析/02scoped_lock源码解析.md)

## 保护共享数据

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# `std::scope_lock` 的源码实现与解析
# `std::scoped_lock` 的源码实现与解析

本单章专门介绍标准库在 C++17 引入的类模板 `std::scope_lock` 的实现,让你对它再无疑问。
本单章专门介绍标准库在 C++17 引入的类模板 `std::scoped_lock` 的实现,让你对它再无疑问。

这会涉及到不少的模板技术,这没办法,就如同我们先前聊 [`std::thread` 的构造与源码分析](01thread的构造与源码解析.md)最后说的:“**不会模板,你阅读标准库源码,是无稽之谈**”。

我们还是一样的,以 MSVC STL 的实现的 [`std::scope_lock`](https://github.com/microsoft/STL/blob/main/stl/inc/mutex#L476-L528) 代码进行讲解,不用担心,我们也查看了 [`libstdc++`](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/mutex#L743-L802) 、[`libc++`](https://github.com/llvm/llvm-project/blob/main/libcxx/include/mutex#L424-L488)的实现,并没有太多区别,更多的是一些风格上的。而且个人觉得 MSVC 的实现是最简单直观的。
我们还是一样的,以 MSVC STL 的实现的 [`std::scoped_lock`](https://github.com/microsoft/STL/blob/main/stl/inc/mutex#L476-L528) 代码进行讲解,不用担心,我们也查看了 [`libstdc++`](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/mutex#L743-L802) 、[`libc++`](https://github.com/llvm/llvm-project/blob/main/libcxx/include/mutex#L424-L488)的实现,并没有太多区别,更多的是一些风格上的。而且个人觉得 MSVC 的实现是最简单直观的。

## `std:scope_lock` 的数据成员
## `std:scoped_lock` 的数据成员

`std::scope_lock` 是一个类模板,它有两个特化,也就是有三个版本,其中的数据成员也是不同的。
`std::scoped_lock` 是一个类模板,它有两个特化,也就是有三个版本,其中的数据成员也是不同的。

1. 主模板,是一个可变参数类模板,声明了一个类型形参包 `_Mutexes`,**存储了一个 `std::tuple`**,具体类型根据类型形参包决定。

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy