13using namespace std::chrono_literals;
15TEST(recurrent_shared_future, use_case) {
16 using namespace carla;
22 constexpr size_t number_of_threads = 12u;
23 constexpr size_t number_of_openings = 40u;
25 std::atomic_size_t count{0u};
27 std::atomic_bool done{
false};
32 auto result = future.
WaitFor(1s);
34 ASSERT_TRUE(result.has_value());
36 ASSERT_EQ(*result, 42);
42 std::this_thread::sleep_for(100ms);
44 for (
auto i = 0u; i < number_of_openings - 1u; ++i) {
47 std::this_thread::sleep_for(10ms);
56 ASSERT_EQ(count, number_of_openings * number_of_threads);
59TEST(recurrent_shared_future, timeout) {
60 using namespace carla;
64 auto result = future.
WaitFor(1ns);
66 ASSERT_FALSE(result.has_value());
69TEST(recurrent_shared_future, exception) {
70 using namespace carla;
76 const std::string message =
"Uh oh an exception!";
79 std::this_thread::sleep_for(10ms);
86 }
catch (
const std::exception &e) {
88 ASSERT_STREQ(e.what(), message.c_str());
这个类类似于共享未来(shared future)的使用方式,但是它的值可以被设置任意次数的值。 未来设计模式的核心思想是异步调用。 Future接口象征着异步执行任务的结果即执行一个耗时任务完全可以另...
void SetException(ExceptionT &&exception)
设置一个异常,这个异常将会被抛给所有正在等待的线程
void SetValue(const T2 &value)
设置值并通知所有等待的线程
boost::optional< T > WaitFor(time_duration timeout)
等待直到下一个值被设置。任意数量的线程可以同时等待。
void CreateThreads(size_t count, F functor)
void CreateThread(F &&functor)
TEST(recurrent_shared_future, use_case)