47 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// UNSUPPORTED: c++03, c++11, c++14, c++1711 12// <semaphore>13 14#include <semaphore>15#include <thread>16#include <chrono>17#include <cassert>18 19#include "make_test_thread.h"20#include "test_macros.h"21 22int main(int, char**)23{24 auto const start = std::chrono::steady_clock::now();25 26 std::counting_semaphore<> s(0);27 28 assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));29 assert(!s.try_acquire_for(std::chrono::milliseconds(250)));30 31 std::thread t = support::make_test_thread([&](){32 std::this_thread::sleep_for(std::chrono::milliseconds(250));33 s.release();34 std::this_thread::sleep_for(std::chrono::milliseconds(250));35 s.release();36 });37 38 assert(s.try_acquire_until(start + std::chrono::seconds(2)));39 assert(s.try_acquire_for(std::chrono::seconds(2)));40 t.join();41 42 auto const end = std::chrono::steady_clock::now();43 assert(end - start < std::chrono::seconds(10));44 45 return 0;46}47