brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 8067438 Raw
148 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++0311 12// <future>13 14// class shared_future<R>15 16// template <class Rep, class Period>17//   future_status18//   wait_for(const chrono::duration<Rep, Period>& rel_time) const;19 20#include <cassert>21#include <chrono>22#include <future>23 24#include "make_test_thread.h"25#include "test_macros.h"26 27typedef std::chrono::milliseconds ms;28 29static const ms sleepTime(500);30static const ms waitTime(5000);31 32void func1(std::promise<int> p)33{34  std::this_thread::sleep_for(sleepTime);35  p.set_value(3);36}37 38int j = 0;39 40void func3(std::promise<int&> p)41{42  std::this_thread::sleep_for(sleepTime);43  j = 5;44  p.set_value(j);45}46 47void func5(std::promise<void> p)48{49  std::this_thread::sleep_for(sleepTime);50  p.set_value();51}52 53int main(int, char**)54{55  typedef std::chrono::high_resolution_clock Clock;56 57  {58    typedef int T;59    std::promise<T> p;60    std::shared_future<T> f = p.get_future();61    support::make_test_thread(func1, std::move(p)).detach();62    assert(f.valid());63    assert(f.wait_for(ms(1)) == std::future_status::timeout);64    assert(f.valid());65    assert(f.wait_for(waitTime) == std::future_status::ready);66    assert(f.valid());67    f.wait();68    assert(f.valid());69  }70  {71    typedef int& T;72    std::promise<T> p;73    std::shared_future<T> f = p.get_future();74    support::make_test_thread(func3, std::move(p)).detach();75    assert(f.valid());76    assert(f.wait_for(ms(1)) == std::future_status::timeout);77    assert(f.valid());78    assert(f.wait_for(waitTime) == std::future_status::ready);79    assert(f.valid());80    f.wait();81    assert(f.valid());82  }83  {84    typedef void T;85    std::promise<T> p;86    std::shared_future<T> f = p.get_future();87    support::make_test_thread(func5, std::move(p)).detach();88    assert(f.valid());89    assert(f.wait_for(ms(1)) == std::future_status::timeout);90    assert(f.valid());91    assert(f.wait_for(waitTime) == std::future_status::ready);92    assert(f.valid());93    f.wait();94    assert(f.valid());95  }96 97  {98    typedef int T;99    std::promise<T> p;100    std::shared_future<T> f = p.get_future();101    Clock::time_point t0 = Clock::now();102    support::make_test_thread(func1, std::move(p)).detach();103    assert(f.valid());104    assert(f.wait_for(ms(1)) == std::future_status::timeout);105    assert(f.valid());106    f.wait();107    Clock::time_point t1 = Clock::now();108    assert(f.valid());109    assert(t1 - t0 >= sleepTime);110    assert(f.wait_for(waitTime) == std::future_status::ready);111    assert(f.valid());112  }113  {114    typedef int& T;115    std::promise<T> p;116    std::shared_future<T> f = p.get_future();117    Clock::time_point t0 = Clock::now();118    support::make_test_thread(func3, std::move(p)).detach();119    assert(f.valid());120    assert(f.wait_for(ms(1)) == std::future_status::timeout);121    assert(f.valid());122    f.wait();123    Clock::time_point t1 = Clock::now();124    assert(f.valid());125    assert(t1 - t0 >= sleepTime);126    assert(f.wait_for(waitTime) == std::future_status::ready);127    assert(f.valid());128  }129  {130    typedef void T;131    std::promise<T> p;132    std::shared_future<T> f = p.get_future();133    Clock::time_point t0 = Clock::now();134    support::make_test_thread(func5, std::move(p)).detach();135    assert(f.valid());136    assert(f.wait_for(ms(1)) == std::future_status::timeout);137    assert(f.valid());138    f.wait();139    Clock::time_point t1 = Clock::now();140    assert(f.valid());141    assert(t1 - t0 >= sleepTime);142    assert(f.wait_for(waitTime) == std::future_status::ready);143    assert(f.valid());144  }145 146  return 0;147}148