Skip to content

Commit d24f3a2

Browse files
Fix race condition
Signed-off-by: pedroazeredo04 <[email protected]>
1 parent fe6aca3 commit d24f3a2

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

rclcpp/test/rclcpp/test_clock.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ TEST_F(TestClockWakeup, no_wakeup_on_sim_time) {
178178
TEST_F(TestClockWakeup, multiple_threads_wait_on_one_clock) {
179179
auto clock = std::make_shared<rclcpp::Clock>(RCL_ROS_TIME);
180180

181-
std::vector<bool> thread_finished(10, false);
181+
std::vector<std::atomic_bool> thread_finished(10);
182+
for (std::atomic_bool & finished : thread_finished) {
183+
finished = false;
184+
}
182185

183186
std::vector<std::thread> threads;
184187

@@ -196,7 +199,7 @@ TEST_F(TestClockWakeup, multiple_threads_wait_on_one_clock) {
196199
// wait a bit so all threads can execute the sleep_until
197200
std::this_thread::sleep_for(std::chrono::milliseconds(500));
198201

199-
for (const bool & finished : thread_finished) {
202+
for (const std::atomic_bool & finished : thread_finished) {
200203
EXPECT_FALSE(finished);
201204
}
202205

@@ -207,7 +210,7 @@ TEST_F(TestClockWakeup, multiple_threads_wait_on_one_clock) {
207210
bool threads_finished = false;
208211
while (!threads_finished && start_time + std::chrono::seconds(1) > cur_time) {
209212
threads_finished = true;
210-
for (const bool finished : thread_finished) {
213+
for (const std::atomic_bool & finished : thread_finished) {
211214
if (!finished) {
212215
threads_finished = false;
213216
}
@@ -217,7 +220,7 @@ TEST_F(TestClockWakeup, multiple_threads_wait_on_one_clock) {
217220
cur_time = std::chrono::steady_clock::now();
218221
}
219222

220-
for (const bool finished : thread_finished) {
223+
for (const std::atomic_bool & finished : thread_finished) {
221224
EXPECT_TRUE(finished);
222225
}
223226

0 commit comments

Comments
 (0)