book: revise wording about "stack unwinding"

This commit is contained in:
mq白
2024-05-21 13:14:12 +08:00
committed by GitHub
parent 78b1cdf6bb
commit af45678ad4
2 changed files with 6 additions and 2 deletions

View File

@@ -67,7 +67,9 @@ int main() {
```
Because C++ guarantees that all stack objects will be destroyed at the end of the declaration period, such code is also extremely safe.
Whether `critical_section()` returns normally or if an exception is thrown in the middle, a stack rollback is thrown, and `unlock()` is automatically called.
Whether `critical_section()` returns normally or if an exception is thrown in the middle, a stack unwinding is thrown, and `unlock()` is automatically called.
> An exception is thrown and not caught (it is implementation-defined whether any stack unwinding is done in this case).
`std::unique_lock` is more flexible than `std::lock_guard`.
Objects of `std::unique_lock` manage the locking and unlocking operations on the `mutex` object with exclusive ownership (no other `unique_lock` objects owning the ownership of a `mutex` object). So in concurrent programming, it is recommended to use `std::unique_lock`.

View File

@@ -68,7 +68,9 @@ int main() {
```
由于 C++ 保证了所有栈对象在生命周期结束时会被销毁,所以这样的代码也是异常安全的。
无论 `critical_section()` 正常返回、还是在中途抛出异常,都会引发栈回退,也就自动调用了 `unlock()`
无论 `critical_section()` 正常返回、还是在中途抛出异常,都会引发栈回,也就自动调用了 `unlock()`
> 没有捕获抛出的异常(此时由实现定义是否进行栈回溯)。
`std::unique_lock` 则是相对于 `std::lock_guard` 出现的,`std::unique_lock` 更加灵活,
`std::unique_lock` 的对象会以独占所有权(没有其他的 `unique_lock` 对象同时拥有某个 `mutex` 对象的所有权)