[libc++] Fix wait_on_destruct.pass.cpp hanging sometimes (#146240)
This test was deadlocking on my machine. It seems to me the intention of `in_async.wait(...)` was to wait for the value to be set to true, which requires a call of `wait(false)` (waits if value matches argument). ~As a drive by change scoped_lock to unique_lock, since there shouldn't be any functional difference between the two in this test.~ I've addressed the issues with the `in_async` by switching to a condition variable instead, since my first attempt at fixing this with `in_async` wasn't sufficient.
This commit is contained in:
@@ -20,22 +20,24 @@
|
||||
#include <atomic>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <thread>
|
||||
|
||||
|
||||
std::mutex mux;
|
||||
|
||||
int main(int, char**) {
|
||||
using namespace std::chrono_literals;
|
||||
std::condition_variable cond;
|
||||
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);
|
||||
auto v = std::async(std::launch::async, [&cond, value = 1]() mutable {
|
||||
std::unique_lock thread_lock(mux);
|
||||
cond.notify_all();
|
||||
thread_lock.unlock();
|
||||
|
||||
value = 4;
|
||||
(void)value;
|
||||
});
|
||||
in_async.wait(true);
|
||||
lock.unlock();
|
||||
cond.wait(lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user