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//名为“test.h”的头文件。
9#include <carla/RecurrentSharedFuture.h>//这里是包含了名为“RecurrentSharedFuture.h”的头文件,这个头文件可能是来自名为“carla”的库或者模块。这个头文件中的内容可能包含与循环共享未来(Recurrent Shared Future,根据文件名推测)相关的类定义、函数声明等内容。
10#include <carla/ThreadGroup.h>//包含与线程组相关的定义,例如创建、管理线程组的类或者函数等内容。
11#include <atomic>
12//包含了<atomic>头文件。
13using namespace std::chrono_literals;
14// 测试 RecurrentSharedFuture 的用例
15TEST(recurrent_shared_future, use_case) {
16 using namespace carla;
17 // 创建一个线程组
18 ThreadGroup threads;
19 // 创建一个 RecurrentSharedFuture 对象
21// 定义常量,线程数量和打开次数
22 constexpr size_t number_of_threads = 12u;
23 constexpr size_t number_of_openings = 40u;
24// 原子变量,用于计数
25 std::atomic_size_t count{0u};
26 // 原子变量,用于标记是否完成
27 std::atomic_bool done{false};
28// 创建多个线程,每个线程执行以下逻辑
29 threads.CreateThreads(number_of_threads, [&]() {
30 while (!done) {
31 // 等待未来对象的值,超时时间为 1 秒
32 auto result = future.WaitFor(1s);
33 // 断言结果有值
34 ASSERT_TRUE(result.has_value());
35 // 断言结果的值为 42
36 ASSERT_EQ(*result, 42);
37 // 增加计数
38 ++count;
39 }
40 });
41// 主线程睡眠 100 毫秒
42 std::this_thread::sleep_for(100ms);
43 // 设置未来对象的值为 42,多次重复此操作
44 for (auto i = 0u; i < number_of_openings - 1u; ++i) {
45 future.SetValue(42);
46 // 主线程睡眠 10 毫秒
47 std::this_thread::sleep_for(10ms);
48 }
49 // 标记完成
50 done = true;
51 // 再次设置未来对象的值为 42
52 future.SetValue(42);
53 // 等待所有线程完成
54 threads.JoinAll();
55 // 断言计数等于线程数量乘以打开次数
56 ASSERT_EQ(count, number_of_openings * number_of_threads);
57}
58// 测试 RecurrentSharedFuture 的超时情况
59TEST(recurrent_shared_future, timeout) {
60 using namespace carla;
61 // 创建一个 RecurrentSharedFuture 对象
63 // 等待未来对象的值,超时时间为 1 纳秒
64 auto result = future.WaitFor(1ns);
65 // 断言结果没有值,因为超时时间很短
66 ASSERT_FALSE(result.has_value());
67}
68// 测试 RecurrentSharedFuture 的异常情况
69TEST(recurrent_shared_future, exception) {
70 using namespace carla;
71 // 创建一个线程组
72 ThreadGroup threads;
73 // 创建一个 RecurrentSharedFuture 对象
75 // 定义异常消息
76 const std::string message = "Uh oh an exception!";
77// 创建一个线程,在该线程中设置未来对象的异常
78 threads.CreateThread([&]() {
79 std::this_thread::sleep_for(10ms);
80 future.SetException(std::runtime_error(message));
81 });
82
83 try {
84 // 等待未来对象的值,如果有异常则抛出
85 future.WaitFor(1s);
86 } catch (const std::exception &e) {
87 // 断言异常消息与预期一致
88 ASSERT_STREQ(e.what(), message.c_str());
89 }
90}
这个类类似于共享未来(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)
Definition ThreadGroup.h:36
void CreateThread(F &&functor)
Definition ThreadGroup.h:31
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
TEST(recurrent_shared_future, use_case)