15a83710eSEric Fiselier //===----------------------------------------------------------------------===// 25a83710eSEric Fiselier // 357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65a83710eSEric Fiselier // 75a83710eSEric Fiselier //===----------------------------------------------------------------------===// 85a83710eSEric Fiselier // 9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads 105a83710eSEric Fiselier 115a83710eSEric Fiselier // <condition_variable> 125a83710eSEric Fiselier 135a83710eSEric Fiselier // class condition_variable; 145a83710eSEric Fiselier 155a83710eSEric Fiselier // ~condition_variable(); 165a83710eSEric Fiselier 175a83710eSEric Fiselier #include <condition_variable> 185a83710eSEric Fiselier #include <mutex> 195a83710eSEric Fiselier #include <thread> 205a83710eSEric Fiselier #include <cassert> 215a83710eSEric Fiselier 2256462801SLouis Dionne #include "make_test_thread.h" 237fc6a556SMarshall Clow #include "test_macros.h" 247fc6a556SMarshall Clow 255a83710eSEric Fiselier std::condition_variable* cv; 265a83710eSEric Fiselier std::mutex m; 275a83710eSEric Fiselier typedef std::unique_lock<std::mutex> Lock; 285a83710eSEric Fiselier 295a83710eSEric Fiselier bool f_ready = false; 305a83710eSEric Fiselier bool g_ready = false; 315a83710eSEric Fiselier f()325a83710eSEric Fiseliervoid f() 335a83710eSEric Fiselier { 345a83710eSEric Fiselier Lock lk(m); 355a83710eSEric Fiselier f_ready = true; 365a83710eSEric Fiselier cv->notify_one(); 375a83710eSEric Fiselier delete cv; 385a83710eSEric Fiselier } 395a83710eSEric Fiselier g()405a83710eSEric Fiseliervoid g() 415a83710eSEric Fiselier { 425a83710eSEric Fiselier Lock lk(m); 435a83710eSEric Fiselier g_ready = true; 445a83710eSEric Fiselier cv->notify_one(); 455a83710eSEric Fiselier while (!f_ready) 465a83710eSEric Fiselier cv->wait(lk); 475a83710eSEric Fiselier } 485a83710eSEric Fiselier main(int,char **)492df59c50SJF Bastienint main(int, char**) 505a83710eSEric Fiselier { 515a83710eSEric Fiselier cv = new std::condition_variable; 5256462801SLouis Dionne std::thread th2 = support::make_test_thread(g); 535a83710eSEric Fiselier Lock lk(m); 545a83710eSEric Fiselier while (!g_ready) 555a83710eSEric Fiselier cv->wait(lk); 565a83710eSEric Fiselier lk.unlock(); 5756462801SLouis Dionne std::thread th1 = support::make_test_thread(f); 585a83710eSEric Fiselier th1.join(); 595a83710eSEric Fiselier th2.join(); 602df59c50SJF Bastien 612df59c50SJF Bastien return 0; 625a83710eSEric Fiselier } 63