Fix template parameter pack handling in ThreadPool
authorTeresa Johnson <tejohnson@google.com>
Tue, 15 Dec 2015 04:44:02 +0000 (04:44 +0000)
committerTeresa Johnson <tejohnson@google.com>
Tue, 15 Dec 2015 04:44:02 +0000 (04:44 +0000)
Fixes passing of template parameter pack via std::forward and add
unittest.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255617 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/ThreadPool.h
unittests/Support/ThreadPool.cpp

index 35dc20867a4edc7ea129b3cf20e475b0f001069f..85c062179f056bec14c144b43242a965c8cc884e 100644 (file)
@@ -66,7 +66,7 @@ public:
   template <typename Function, typename... Args>
   inline std::shared_future<VoidTy> async(Function &&F, Args &&... ArgList) {
     auto Task =
-        std::bind(std::forward<Function>(F), std::forward<Args...>(ArgList...));
+        std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...);
 #ifndef _MSC_VER
     return asyncImpl(std::move(Task));
 #else
index d36341e425df72b1defd0ecee771a358a333243c..5457cdc1c683aea6fb55c17a671e45ec0dce3bff 100644 (file)
@@ -44,6 +44,20 @@ TEST(ThreadPoolTest, AsyncBarrier) {
   ASSERT_EQ(5, checked_in);
 }
 
+static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
+
+TEST(ThreadPoolTest, AsyncBarrierArgs) {
+  // Test that async works with a function requiring multiple parameters.
+  std::atomic_int checked_in{0};
+
+  ThreadPool Pool;
+  for (size_t i = 0; i < 5; ++i) {
+    Pool.async(TestFunc, std::ref(checked_in), i);
+  }
+  Pool.wait();
+  ASSERT_EQ(10, checked_in);
+}
+
 TEST(ThreadPoolTest, Async) {
   ThreadPool Pool;
   std::atomic_int i{0};