Skip to content

Commit

Permalink
Added mutex for subscriber's message updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanFabian committed Mar 21, 2022
1 parent a9d4931 commit 8af695e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/qml_ros_plugin/subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected slots:
ros::Subscriber subscriber_;
ros_babel_fish::BabelFish babel_fish_;
ros_babel_fish::BabelFishMessage::ConstPtr last_message_;
ros_babel_fish::BabelFishMessage::ConstPtr current_message_;
std::mutex message_mutex_;
QTimer throttle_timer_;
bool running_;
bool is_subscribed_;
Expand Down
14 changes: 9 additions & 5 deletions src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,22 @@ void Subscriber::shutdown()

void Subscriber::messageCallback( const ros_babel_fish::BabelFishMessage::ConstPtr &msg )
{
std::lock_guard<std::mutex> lock( message_mutex_ );
last_message_ = msg;
}

void Subscriber::updateMessage()
{
if (current_message_ == last_message_) return;
current_message_ = last_message_;
TranslatedMessage::Ptr translated = babel_fish_.translateMessage( current_message_ );
std::unique_lock<std::mutex> lock( message_mutex_ );
if ( !last_message_ ) return;
ros_babel_fish::BabelFishMessage::ConstPtr msg = std::move( last_message_ );
last_message_ = nullptr;
lock.unlock(); // Don't need the mutex anymore
TranslatedMessage::Ptr translated = babel_fish_.translateMessage( msg );
message_ = msgToMap( translated, translated->translated_message->as<CompoundMessage>());
if (current_message_->dataType() != message_type_.toStdString())
if ( msg->dataType() != message_type_.toStdString())
{
message_type_ = QString::fromStdString(current_message_->dataType());
message_type_ = QString::fromStdString( msg->dataType());
emit messageTypeChanged();
}
emit messageChanged();
Expand Down

0 comments on commit 8af695e

Please sign in to comment.