Skip to content

Commit

Permalink
Merge pull request #1727 from mavlink/pr-fix-lazy-plugin
Browse files Browse the repository at this point in the history
Make LazyPlugin thread-safe
  • Loading branch information
julianoes authored Apr 1, 2022
2 parents 53dbea8 + 2ee1a55 commit 4dcee2b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/mavsdk/core/lazy_plugin.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>
#include <mutex>

#include <mavsdk.h>

Expand All @@ -12,18 +13,20 @@ template<typename Plugin> class LazyPlugin {

Plugin* maybe_plugin()
{
if (_action == nullptr) {
std::lock_guard<std::mutex> lock(_mutex);
if (_plugin == nullptr) {
if (_mavsdk.systems().empty()) {
return nullptr;
}
_action = std::make_unique<Plugin>(_mavsdk.systems()[0]);
_plugin = std::make_unique<Plugin>(_mavsdk.systems()[0]);
}
return _action.get();
return _plugin.get();
}

private:
Mavsdk& _mavsdk;
std::unique_ptr<Plugin> _action{};
std::unique_ptr<Plugin> _plugin{};
std::mutex _mutex{};
};

} // namespace mavsdk::mavsdk_server

0 comments on commit 4dcee2b

Please sign in to comment.