1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
12 // This test requires the fix for http://llvm.org/PR38682 (616ef1863fae). We mark the
13 // test as UNSUPPORTED instead of XFAIL because the test doesn't fail consistently.
14 // UNSUPPORTED: using-built-library-before-llvm-10
16 // This test is designed to cause and allow TSAN to detect a race condition
17 // in std::async, as reported in https://llvm.org/PR38682.
25 #include "test_macros.h"
28 static int worker(std::vector
<int> const& data
) {
29 return std::accumulate(data
.begin(), data
.end(), 0);
32 static int& worker_ref(int& i
) { return i
; }
34 static void worker_void() { }
36 int main(int, char**) {
39 std::vector
<int> const v
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
40 for (int i
= 0; i
!= 20; ++i
) {
41 std::future
<int> fut
= std::async(std::launch::async
, worker
, v
);
42 int answer
= fut
.get();
49 for (int i
= 0; i
!= 20; ++i
) {
50 std::future
<int&> fut
= std::async(std::launch::async
, worker_ref
, std::ref(i
));
51 int& answer
= fut
.get();
58 for (int i
= 0; i
!= 20; ++i
) {
59 std::future
<void> fut
= std::async(std::launch::async
, worker_void
);