#include "Halide.h"
#include <stdio.h>
#include <future>

using namespace Halide;

static std::atomic<int> foo;

int main(int argc, char **argv) {
    // Test if the compiler itself is thread-safe. This test is
    // intended to be run in a thread-sanitizer.
    std::vector<std::future<void>> futures;
    for (int i = 0; i < 1000; i++) {
        futures.emplace_back(std::async(std::launch::async, []{
            Func f;
            Var x;
            f(x) = x;
            f.realize(100);
        }));
    }

    for (auto &f : futures) {
        f.wait();
    }

    printf("Success!\n");

    return 0;
}

#include "Halide.h"
#include <stdio.h>
#include <thread>

using namespace Halide;

int main(int argc, char **argv) {
    // Test if the compiler itself is thread-safe. This test is
    // intended to be run in a thread-sanitizer.
    std::vector<std::thread> threads;
    for (int i = 0; i < 1000; i++) {
        threads.emplace_back([]{
            Func f;
            Var x;
            f(x) = x;
            f.realize(100);
        });
    }

    for (auto &t : threads) {
        t.join();
    }

    printf("Success!\n");

    return 0;
}

--- 
+++ 
@@ -1,24 +1,26 @@
 #include "Halide.h"
 #include <stdio.h>
-#include <thread>
+#include <future>
 
 using namespace Halide;
+
+static std::atomic<int> foo;
 
 int main(int argc, char **argv) {
     // Test if the compiler itself is thread-safe. This test is
     // intended to be run in a thread-sanitizer.
-    std::vector<std::thread> threads;
+    std::vector<std::future<void>> futures;
     for (int i = 0; i < 1000; i++) {
-        threads.emplace_back([]{
+        futures.emplace_back(std::async(std::launch::async, []{
             Func f;
             Var x;
             f(x) = x;
             f.realize(100);
-        });
+        }));
     }
 
-    for (auto &t : threads) {
-        t.join();
+    for (auto &f : futures) {
+        f.wait();
     }
 
     printf("Success!\n");
Use std::async() instead of std::thread()

It is guaranteed to use a pool-like behavior; to ensure that every task
is executed by spawning a new thread, we must explicitly specify
std::launch::async.

Former-commit-id: 4236c652b3d581ce8b6851efd4653cb815c4ac70