62 lines · cpp
1//===----------------------------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: no-threads10// UNSUPPORTED: c++0311 12// This test is designed to cause and allow TSAN to detect a race condition13// in std::async, as reported in https://llvm.org/PR38682.14 15#include <cassert>16#include <functional>17#include <future>18#include <numeric>19#include <vector>20 21#include "test_macros.h"22 23 24static int worker(std::vector<int> const& data) {25 return std::accumulate(data.begin(), data.end(), 0);26}27 28static int& worker_ref(int& i) { return i; }29 30static void worker_void() { }31 32int main(int, char**) {33 // future<T>34 {35 std::vector<int> const v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};36 for (int i = 0; i != 20; ++i) {37 std::future<int> fut = std::async(std::launch::async, worker, v);38 int answer = fut.get();39 assert(answer == 55);40 }41 }42 43 // future<T&>44 {45 for (int i = 0; i != 20; ++i) {46 std::future<int&> fut = std::async(std::launch::async, worker_ref, std::ref(i));47 int& answer = fut.get();48 assert(answer == i);49 }50 }51 52 // future<void>53 {54 for (int i = 0; i != 20; ++i) {55 std::future<void> fut = std::async(std::launch::async, worker_void);56 fut.get();57 }58 }59 60 return 0;61}62