You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: md/02使用线程.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -361,8 +361,8 @@ class thread_guard{
361
361
public:
362
362
explicit thread_guard(std::thread& t) :m_t{ t } {}
363
363
~thread_guard(){
364
-
std::puts("析构"); // 打印 不用在乎
365
-
if (m_t.joinable()) { // 没有关联活跃线程
364
+
std::puts("析构"); // 打印日志 不用在乎
365
+
if (m_t.joinable()) { // 线程对象当前关联了活跃线程
366
366
m_t.join();
367
367
}
368
368
}
@@ -377,23 +377,23 @@ void f(){
377
377
}
378
378
```
379
379
380
-
函数 f 执行完毕,局部对象就要逆序销毁了。因此,thread_guard 对象 g 是第一个被销毁的,**调用析构函数**。**即使函数 f2() 抛出了一个异常,这个销毁依然会发生(前提是你捕获了这个异常)**。这确保了线程对象 t 所关联的线程正常的执行完毕以及线程对象的正常析构。[测试代码](https://godbolt.org/z/MaWjW73P4)。
380
+
函数 f 执行完毕,局部对象就要逆序销毁了。因此,thread_guard 对象 g 是第一个被销毁的,**调用析构函数**。**即使函数 f2() 抛出了一个异常,这个销毁依然会发生(前提是你捕获了这个异常)**。这确保了线程对象 t 所关联的线程正常的执行完毕以及线程对象的正常析构。[测试代码](https://godbolt.org/z/hn7Gced84)。
0 commit comments