brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 1712490 Raw
68 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: no-threads10 11// <condition_variable>12 13// class condition_variable_any;14 15// void notify_all();16 17#include <condition_variable>18#include <mutex>19#include <thread>20#include <vector>21#include <atomic>22#include <cassert>23 24#include "make_test_thread.h"25#include "test_macros.h"26 27std::condition_variable_any cv;28 29typedef std::timed_mutex L0;30typedef std::unique_lock<L0> L1;31 32L0 m0;33 34const unsigned threadCount = 2;35bool pleaseExit = false;36std::atomic<unsigned> notReady;37 38void helper() {39  L1 lk(m0);40  --notReady;41  while (pleaseExit == false)42    cv.wait(lk);43}44 45int main(int, char**)46{47  notReady = threadCount;48  std::vector<std::thread> threads(threadCount);49  for (unsigned i = 0; i < threadCount; i++)50    threads[i] = support::make_test_thread(helper);51  {52    while (notReady > 0)53      std::this_thread::yield();54    // At this point, both threads have had a chance to acquire the lock and are55    // either waiting on the condition variable or about to wait.56    L1 lk(m0);57    pleaseExit = true;58    // POSIX does not guarantee reliable scheduling if notify_all is called59    // without the lock being held.60    cv.notify_all();61  }62  // The test will hang if not all of the threads were woken.63  for (unsigned i = 0; i < threadCount; i++)64    threads[i].join();65 66  return 0;67}68