CARLA
 
载入中...
搜索中...
未找到
test_recurrent_shared_future.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "test.h"
8
10#include <carla/ThreadGroup.h>
11#include <atomic>
12
13using namespace std::chrono_literals;
14
15TEST(recurrent_shared_future, use_case) {
16 using namespace carla;
17 ThreadGroup threads;
19
20 constexpr size_t number_of_threads = 12u;
21 constexpr size_t number_of_openings = 40u;
22
23 std::atomic_size_t count{0u};
24 std::atomic_bool done{false};
25
26 threads.CreateThreads(number_of_threads, [&]() {
27 while (!done) {
28 auto result = future.WaitFor(1s);
29 ASSERT_TRUE(result.has_value());
30 ASSERT_EQ(*result, 42);
31 ++count;
32 }
33 });
34
35 std::this_thread::sleep_for(100ms);
36 for (auto i = 0u; i < number_of_openings - 1u; ++i) {
37 future.SetValue(42);
38 std::this_thread::sleep_for(10ms);
39 }
40 done = true;
41 future.SetValue(42);
42 threads.JoinAll();
43 ASSERT_EQ(count, number_of_openings * number_of_threads);
44}
45
46TEST(recurrent_shared_future, timeout) {
47 using namespace carla;
49 auto result = future.WaitFor(1ns);
50 ASSERT_FALSE(result.has_value());
51}
52
53TEST(recurrent_shared_future, exception) {
54 using namespace carla;
55 ThreadGroup threads;
57 const std::string message = "Uh oh an exception!";
58
59 threads.CreateThread([&]() {
60 std::this_thread::sleep_for(10ms);
61 future.SetException(std::runtime_error(message));
62 });
63
64 try {
65 future.WaitFor(1s);
66 } catch (const std::exception &e) {
67 ASSERT_STREQ(e.what(), message.c_str());
68 }
69}
This class is meant to be used similar to a shared future, but the value can be set any number of tim...
void SetException(ExceptionT &&exception)
Set a exception, this exception will be thrown on all the threads waiting.
void SetValue(const T2 &value)
Set the value and notify all waiting threads.
boost::optional< T > WaitFor(time_duration timeout)
Wait until the next value is set.
void CreateThreads(size_t count, F functor)
Definition ThreadGroup.h:32
void CreateThread(F &&functor)
Definition ThreadGroup.h:27
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
TEST(recurrent_shared_future, use_case)