Skip to content

Commit ca83b46

Browse files
committed
Use condition variable approach instead
1 parent 06a816e commit ca83b46

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@
2020
#include <atomic>
2121
#include <future>
2222
#include <mutex>
23+
#include <condition_variable>
24+
#include <thread>
25+
#include <chrono>
2326

2427
std::mutex mux;
2528

2629
int main(int, char**) {
27-
using namespace std::chrono_literals;
30+
std::condition_variable cond;
2831
std::unique_lock lock(mux);
29-
std::atomic<bool> in_async = false;
30-
auto v = std::async(std::launch::async, [&in_async, value = 1]() mutable {
31-
in_async = true;
32-
in_async.notify_all();
32+
auto v = std::async(std::launch::async, [&cond, value = 1]() mutable {
3333
std::unique_lock thread_lock(mux);
34+
cond.notify_all();
35+
thread_lock.unlock();
36+
std::this_thread::sleep_for(std::chrono::seconds(1));
37+
3438
value = 4;
3539
(void)value;
3640
});
37-
in_async.wait(false);
38-
lock.unlock();
41+
cond.wait(lock);
3942

4043
return 0;
4144
}

0 commit comments

Comments
 (0)