Skip to content

Commit

Permalink
Refs 11606. Improve thread safety on DataSharingListener.
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
  • Loading branch information
MiguelCompany committed May 20, 2021
1 parent c46ac4a commit 36edae7
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/cpp/rtps/DataSharing/DataSharingListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void DataSharingListener::run()

void DataSharingListener::start()
{
std::lock_guard<std::mutex> guard(mutex_);

// Check the thread
bool was_running = is_running_.exchange(true);
if (was_running)
Expand All @@ -92,17 +94,26 @@ void DataSharingListener::start()

void DataSharingListener::stop()
{
// Notify the listening thread that is no longer running
bool was_running = is_running_.exchange(false);
if (!was_running)
std::thread* thr = nullptr;

{
return;
std::lock_guard<std::mutex> guard(mutex_);

// Notify the listening thread that is no longer running
bool was_running = is_running_.exchange(false);
if (!was_running)
{
return;
}

thr = listening_thread_;
listening_thread_ = nullptr;
}

// Notify the thread and wait for it to finish
notification_->notify();
listening_thread_->join();
delete listening_thread_;
thr->join();
delete thr;
}

void DataSharingListener::process_new_data ()
Expand Down

0 comments on commit 36edae7

Please sign in to comment.