Skip to content
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

RAII 不是一种语法,第七章描述错误 #283

Closed
Mq-b opened this issue May 20, 2024 · 1 comment · Fixed by #288
Closed

RAII 不是一种语法,第七章描述错误 #283

Mq-b opened this issue May 20, 2024 · 1 comment · Fixed by #288

Comments

@Mq-b
Copy link
Contributor

Mq-b commented May 20, 2024

  • 文件路径:book/zh-cn/07-thread.md

web 位置

原文:

std::mutex 是 C++11 中最基本的 mutex 类,通过实例化 std::mutex 可以创建互斥量, 而通过其成员函数 lock() 可以进行上锁,unlock() 可以进行解锁。 但是在实际编写代码的过程中,最好不去直接调用成员函数, 因为调用成员函数就需要在每个临界区的出口处调用 unlock(),当然,还包括异常。 这时候 C++11 还为互斥量提供了一个 RAII 语法的模板类 std::lock_guard。

RAII(Resource Acquisition Is Initialization)并不是一种语法,而是一种 C++ 编程技术(或者说是一种设计方式)。此处存在误导,应该将“RAII 语法”改为“RAII 机制”或者“RAII 模式”。

也就是:“C++11 还为互斥量提供了一个 RAII 机制的模板类 std::lock_guard”。

如有更好的措辞可再提,不过至少“RAII 语法”完全不正确。

@changkun
Copy link
Owner

Thanks and PR welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants