Skip to content

Commit e6644e7

Browse files
authored
Fix -Wunused-result warning in ThreadPool::AddTask (colmap#4958)
## Summary The finished-task checker in ThreadPool::AddTask calls shared_future::get() only to rethrow task exceptions through Wait()/Stop(); the return value is already available via the returned future. For non-void tasks, get() is [[nodiscard]], which produced -Wunused-result warnings (e.g. for bool and std::optional<InitInfo> tasks). Discard the value explicitly with a (void) cast, matching the existing idiom in undistorters.cc. Apply the same discard to the NoArgReturn and ArgReturn tests, and add WaitPropagatesExceptionWithReturnValue to cover exception propagation from a value-returning task. ## Tests - Rebuilt undistorters.cc and incremental_mapper_impl.cc with no -Wunused-result warnings. - colmap_util_threading_test: 35 tests passed, including the new test.
1 parent 30a1a81 commit e6644e7

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎src/colmap/util/threading.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ auto ThreadPool::AddTask(func_t&& f, args_t&&... args)
360360
}
361361
tasks_.emplace([task = std::move(task), result]() {
362362
(*task)();
363-
return [result = std::move(result)]() { result.get(); };
363+
return [result = std::move(result)]() { (void)result.get(); };
364364
});
365365
}
366366

‎src/colmap/util/threading_test.cc‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ TEST(ThreadPool, NoArgReturn) {
523523
}
524524

525525
for (auto& future : futures) {
526-
future.get();
526+
(void)future.get();
527527
}
528528
}
529529

@@ -543,7 +543,7 @@ TEST(ThreadPool, ArgReturn) {
543543
}
544544

545545
for (auto& future : futures) {
546-
future.get();
546+
(void)future.get();
547547
}
548548
}
549549

@@ -647,6 +647,13 @@ TEST(ThreadPool, WaitPropagatesException) {
647647
EXPECT_NO_THROW(pool.Wait());
648648
}
649649

650+
TEST(ThreadPool, WaitPropagatesExceptionWithReturnValue) {
651+
ThreadPool pool(1);
652+
pool.AddTask([]() -> int { throw std::runtime_error("Error"); });
653+
EXPECT_THROW(pool.Wait(), AggregateException);
654+
EXPECT_NO_THROW(pool.Wait());
655+
}
656+
650657
TEST(ThreadPool, WaitPropagatesMultipleExceptions) {
651658
ThreadPool pool(1);
652659
pool.AddTask([]() { throw std::runtime_error("Error 1"); });

0 commit comments

Comments
 (0)