CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
test
client
test_recurrent_shared_future.cpp
Go to the documentation of this file.
1
// Copyright (c) 2026 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
9
#include <
carla/RecurrentSharedFuture.h
>
10
#include <
carla/ThreadGroup.h
>
11
#include <atomic>
12
13
using namespace
std::chrono_literals;
14
15
TEST
(recurrent_shared_future, use_case) {
16
using namespace
carla
;
17
ThreadGroup
threads;
18
RecurrentSharedFuture<int>
future;
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
46
TEST
(recurrent_shared_future, timeout) {
47
using namespace
carla
;
48
RecurrentSharedFuture<int>
future;
49
auto
result = future.
WaitFor
(1ns);
50
ASSERT_FALSE(result.has_value());
51
}
52
53
TEST
(recurrent_shared_future, exception) {
54
using namespace
carla
;
55
ThreadGroup
threads;
56
RecurrentSharedFuture<int>
future;
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
// The future rethrows a carla::detail::SharedException wrapping the
65
// message; carla::throw_exception is templated on the static type, so the
66
// wrapper reaches the waiter with its type intact (no slicing).
67
ASSERT_THROW(future.
WaitFor
(1s),
carla::detail::SharedException
);
68
}
RecurrentSharedFuture.h
ThreadGroup.h
carla::RecurrentSharedFuture
This class is meant to be used similar to a shared future, but the value can be set any number of tim...
Definition
RecurrentSharedFuture.h:43
carla::RecurrentSharedFuture::SetException
void SetException(ExceptionT &&exception)
Set a exception, this exception will be thrown on all the threads waiting.
Definition
RecurrentSharedFuture.h:139
carla::RecurrentSharedFuture::SetValue
void SetValue(const T2 &value)
Set the value and notify all waiting threads.
Definition
RecurrentSharedFuture.h:128
carla::RecurrentSharedFuture::WaitFor
std::optional< T > WaitFor(time_duration timeout)
Wait until the next value is set.
Definition
RecurrentSharedFuture.h:113
carla::ThreadGroup
Definition
ThreadGroup.h:17
carla::ThreadGroup::JoinAll
void JoinAll()
Definition
ThreadGroup.h:39
carla::ThreadGroup::CreateThreads
void CreateThreads(size_t count, F functor)
Definition
ThreadGroup.h:32
carla::ThreadGroup::CreateThread
void CreateThread(F &&functor)
Definition
ThreadGroup.h:27
carla::detail::SharedException
Definition
RecurrentSharedFuture.h:88
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
test.h
TEST
TEST(agents_types, road_option_values_match_python)
Definition
test_agents.cpp:40
Generated by
1.18.0