Run more stuff in parallel.
authorMilian Wolff <mail@milianw.de>
Mon, 15 Jun 2015 20:26:07 +0000 (22:26 +0200)
committerMilian Wolff <mail@milianw.de>
Mon, 15 Jun 2015 20:26:07 +0000 (22:26 +0200)
tests/threaded.cpp

index f4ca1bb..7c38891 100644 (file)
@@ -1,5 +1,6 @@
 #include <thread>
 #include <future>
+#include <vector>
 
 using namespace std;
 
@@ -14,27 +15,28 @@ int** alloc()
     return block;
 }
 
-bool dealloc(future<int**>&& f)
+void dealloc(future<int**>&& f)
 {
     int** block = f.get();
     for (int i = 0; i < ALLOCS_PER_THREAD; ++i) {
         delete block[i];
     }
     delete[] block;
-    return true;
 }
 
 int main()
 {
+    vector<future<void>> futures;
+    futures.reserve(100 * 4);
     for (int i = 0; i < 100; ++i) {
         auto f1 = async(launch::async, alloc);
         auto f2 = async(launch::async, alloc);
         auto f3 = async(launch::async, alloc);
         auto f4 = async(launch::async, alloc);
-        auto f5 = async(launch::async, dealloc, move(f1));
-        auto f6 = async(launch::async, dealloc, move(f2));
-        auto f7 = async(launch::async, dealloc, move(f3));
-        auto f8 = async(launch::async, dealloc, move(f4));
+        futures.emplace_back(async(launch::async, dealloc, move(f1)));
+        futures.emplace_back(async(launch::async, dealloc, move(f2)));
+        futures.emplace_back(async(launch::async, dealloc, move(f3)));
+        futures.emplace_back(async(launch::async, dealloc, move(f4)));
     }
     return 0;
 }