Files
clang-p2996/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp
Nikolas Klauser 696c0f92e0 [libc++] Don't try to wait on a thread that hasn't started in std::async, take 2 (#130145)
If the creation of a thread fails, this causes an idle loop that will
never end because the thread wasn't started in the first place.

This also adds a test for the regression reported in #125433 to make
sure we're not reintroducing it later.

Fixes #125428
2025-06-26 12:13:19 +02:00

38 lines
1.1 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.
#include <atomic>
#include <future>
#include <mutex>
std::mutex mux;
int main() {
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();
}