Skip to content

Commit

Permalink
core: make LazyPlugin thread-safe
Browse files Browse the repository at this point in the history
Before this, we could potentially create multiple plugins and overwrite
each other.
  • Loading branch information
julianoes committed Mar 31, 2022
1 parent daf6201 commit 2ee1a55
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 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,6 +13,7 @@ template<typename Plugin> class LazyPlugin {

Plugin* maybe_plugin()
{
std::lock_guard<std::mutex> lock(_mutex);
if (_plugin == nullptr) {
if (_mavsdk.systems().empty()) {
return nullptr;
Expand All @@ -24,6 +26,7 @@ template<typename Plugin> class LazyPlugin {
private:
Mavsdk& _mavsdk;
std::unique_ptr<Plugin> _plugin{};
std::mutex _mutex{};
};

} // namespace mavsdk::mavsdk_server

0 comments on commit 2ee1a55

Please sign in to comment.