@@ -47,9 +47,9 @@ namespace mozc {
47
47
// Represents a thread, exposing a subset of `std::thread` APIs.
48
48
//
49
49
// Most notably, threads are undetachable unlike `std::thread`, thus must be
50
- // `join ()`ed before destruction. This means that the `mozc::Thread` instance
51
- // must be retained even for a long-running one, though which may be until
52
- // the end of the process.
50
+ // `Join ()`ed before destruction if `Joinable()` . This means that the
51
+ // `mozc::Thread` instance must be retained even for a long-running one, though
52
+ // which may be until the end of the process.
53
53
//
54
54
// The semantics of the present APIs are mostly the same as `std::thread`
55
55
// counterpart of the same (but lowercase) name, except that the behavior of
@@ -77,6 +77,8 @@ class Thread {
77
77
Thread (Thread &&) noexcept = default ;
78
78
Thread &operator =(Thread &&) noexcept = default ;
79
79
80
+ bool Joinable () const noexcept { return thread_.joinable (); }
81
+
80
82
void Join () { thread_.join (); }
81
83
82
84
private:
@@ -182,7 +184,9 @@ BackgroundFuture<R>::BackgroundFuture(F &&f, Args &&...args)
182
184
183
185
template <class R >
184
186
BackgroundFuture<R>::~BackgroundFuture () {
185
- thread_.Join ();
187
+ if (thread_.Joinable ()) {
188
+ thread_.Join ();
189
+ }
186
190
}
187
191
188
192
template <class R >
@@ -229,7 +233,11 @@ BackgroundFuture<void>::BackgroundFuture(F &&f, Args &&...args)
229
233
done.Notify ();
230
234
}) {}
231
235
232
- inline BackgroundFuture<void >::~BackgroundFuture () { thread_.Join (); }
236
+ inline BackgroundFuture<void >::~BackgroundFuture () {
237
+ if (thread_.Joinable ()) {
238
+ thread_.Join ();
239
+ }
240
+ }
233
241
234
242
inline void BackgroundFuture<void >::Wait() const {
235
243
done_->WaitForNotification ();
0 commit comments