Skip to content

Commit

Permalink
book: revise wording about "stack unwinding"
Browse files Browse the repository at this point in the history
  • Loading branch information
Mq-b authored May 21, 2024
1 parent 78b1cdf commit af45678
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion book/en-us/07-thread.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 3 additions & 1 deletion book/zh-cn/07-thread.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 对象的所有权)
Expand Down

0 comments on commit af45678

Please sign in to comment.