@@ -78,6 +78,14 @@ class RealtimePublisher
78
78
: publisher_(publisher), is_running_(false ), keep_running_(true ), turn_(State::LOOP_NOT_STARTED)
79
79
{
80
80
thread_ = std::thread (&RealtimePublisher::publishingLoop, this );
81
+
82
+ // Wait for the thread to be ready before proceeding
83
+ // This is important to ensure that the thread is properly initialized and ready to handle
84
+ // messages before any other operations are performed on the RealtimePublisher instance.
85
+ while (!thread_.joinable () ||
86
+ turn_.load (std::memory_order_acquire) == State::LOOP_NOT_STARTED) {
87
+ std::this_thread::sleep_for (std::chrono::microseconds (100 ));
88
+ }
81
89
}
82
90
83
91
[[deprecated(
@@ -116,14 +124,14 @@ class RealtimePublisher
116
124
/* *
117
125
* \brief Try to acquire the data lock for non-realtime message publishing
118
126
*
119
- * It first checks if the current state allows non-realtime message publishing (turn_ == NON_REALTIME )
127
+ * It first checks if the current state allows non-realtime message publishing (turn_ == REALTIME )
120
128
* and then attempts to lock
121
129
*
122
130
* \return true if the lock was successfully acquired, false otherwise
123
131
*/
124
132
bool trylock ()
125
133
{
126
- if (turn_.load (std::memory_order_acquire) == State::NON_REALTIME && msg_mutex_.try_lock ()) {
134
+ if (turn_.load (std::memory_order_acquire) == State::REALTIME && msg_mutex_.try_lock ()) {
127
135
return true ;
128
136
} else {
129
137
return false ;
@@ -159,7 +167,7 @@ class RealtimePublisher
159
167
*/
160
168
void unlockAndPublish ()
161
169
{
162
- turn_.store (State::REALTIME , std::memory_order_release);
170
+ turn_.store (State::NON_REALTIME , std::memory_order_release);
163
171
unlock ();
164
172
}
165
173
@@ -192,33 +200,32 @@ class RealtimePublisher
192
200
* \brief Publishing loop (runs in separate thread)
193
201
*
194
202
* This is the main loop for the non-realtime publishing thread. It:
195
- * 1. Waits for new messages (State::REALTIME )
203
+ * 1. Waits for new messages (State::NON_REALTIME )
196
204
* 2. Copies the message data
197
205
* 3. Publishes the message through the ROS publisher
198
- * 4. Returns to State::NON_REALTIME to allow realtime updates
206
+ * 4. Returns to State::REALTIME to allow realtime updates
199
207
*
200
208
* The loop continues until keep_running_ is set to false.
201
209
*/
202
210
void publishingLoop ()
203
211
{
204
212
is_running_ = true ;
205
- turn_.store (State::NON_REALTIME, std::memory_order_release);
206
213
207
214
while (keep_running_) {
208
215
MessageT outgoing;
209
216
210
217
{
218
+ turn_.store (State::REALTIME, std::memory_order_release);
211
219
// Locks msg_ and copies it to outgoing
212
220
std::unique_lock<std::mutex> lock_ (msg_mutex_);
213
- updated_cond_.wait (lock_, [&] { return turn_ == State::REALTIME || !keep_running_; });
221
+ updated_cond_.wait (lock_, [&] { return turn_ == State::NON_REALTIME || !keep_running_; });
214
222
outgoing = msg_;
215
223
}
216
224
217
225
// Sends the outgoing message
218
226
if (keep_running_) {
219
227
publisher_->publish (outgoing);
220
228
}
221
- turn_.store (State::NON_REALTIME, std::memory_order_release);
222
229
}
223
230
is_running_ = false ;
224
231
}
0 commit comments