1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 // UNSUPPORTED: c++98, c++03
12 
13 // <future>
14 
15 // template <class F, class... Args>
16 //     future<typename result_of<F(Args...)>::type>
17 //     async(F&& f, Args&&... args);
18 
19 // template <class F, class... Args>
20 //     future<typename result_of<F(Args...)>::type>
21 //     async(launch policy, F&& f, Args&&... args);
22 
23 // This test is designed to cause and allow TSAN to detect the race condition
24 // reported in PR23293. (http://llvm.org/PR23293).
25 
26 #include <future>
27 #include <chrono>
28 #include <thread>
29 #include <memory>
30 #include <cassert>
31 
f_async()32 int f_async() {
33     typedef std::chrono::milliseconds ms;
34     std::this_thread::sleep_for(ms(200));
35     return 42;
36 }
37 
38 bool ran = false;
39 
f_deferred()40 int f_deferred() {
41     ran = true;
42     return 42;
43 }
44 
test_each()45 void test_each() {
46     {
47         std::future<int> f = std::async(f_async);
48         int const result = f.get();
49         assert(result == 42);
50     }
51     {
52         std::future<int> f = std::async(std::launch::async, f_async);
53         int const result = f.get();
54         assert(result == 42);
55     }
56     {
57         ran = false;
58         std::future<int> f = std::async(std::launch::deferred, f_deferred);
59         assert(ran == false);
60         int const result = f.get();
61         assert(ran == true);
62         assert(result == 42);
63     }
64 }
65 
main()66 int main() {
67     for (int i=0; i < 25; ++i) test_each();
68 }
69