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: https://bugs.llvm.org/show_bug.cgi?id=23293 25 26 #include <future> 27 #include <chrono> 28 #include <thread> 29 #include <memory> 30 #include <cassert> 31 f_async()32int 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()40int f_deferred() { 41 ran = true; 42 return 42; 43 } 44 test_each()45void 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()66int main() { 67 for (int i=0; i < 25; ++i) test_each(); 68 } 69