Files
clang-p2996/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp
Nikolas Klauser 0905767610 [libc++] Address remaining comments from #130145
I've accidentally merged the PR before addressing all
comments. This patch fixes the remaining ones.
2025-06-26 12:34:19 +02:00

42 lines
1.3 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads
// UNSUPPORTED: c++03
// This test uses std::atomic interfaces that are only available in C++20
// UNSUPPORTED: c++11, c++14, c++17
// Make sure that the `future` destructor keeps the data alive until the thread finished. This test fails by triggering
// TSan. It may not be observable by normal means.
// See https://github.com/llvm/llvm-project/pull/125433#issuecomment-2703618927 for more details.
#include <atomic>
#include <future>
#include <mutex>
std::mutex mux;
int main(int, char**) {
using namespace std::chrono_literals;
std::unique_lock lock(mux);
std::atomic<bool> in_async = false;
auto v = std::async(std::launch::async, [&in_async, value = 1]() mutable {
in_async = true;
in_async.notify_all();
std::scoped_lock thread_lock(mux);
value = 4;
(void)value;
});
in_async.wait(true);
lock.unlock();
return 0;
}