book: revise the description of RAII (#288)

This commit is contained in:
mq白
2024-06-01 18:09:32 +08:00
committed by GitHub
parent 2659a5f207
commit 7b57e5e5a1
2 changed files with 3 additions and 2 deletions

View File

@@ -35,7 +35,8 @@ C++11 引入了 `mutex` 相关的类,其所有相关的函数都放在 `<mutex
而通过其成员函数 `lock()` 可以进行上锁,`unlock()` 可以进行解锁。
但是在实际编写代码的过程中,最好不去直接调用成员函数,
因为调用成员函数就需要在每个临界区的出口处调用 `unlock()`,当然,还包括异常。
这时候 C++11 为互斥量提供了一个 RAII 语法的模板类 `std::lock_guard`
C++11 为互斥量提供了一个 RAII 机制的模板类 `std::lock_guard`
RAII 在不失代码简洁性的同时,很好的保证了代码的异常安全性。
在 RAII 用法下,对于临界区的互斥量的创建只需要在作用域的开始部分,例如: