@@ -117,6 +117,7 @@ class Blackboard
117117 void set (const std::string& key, const T& value)
118118 {
119119 std::unique_lock<std::mutex> lock (mutex_);
120+ std::unique_lock<std::mutex> lock_entry (entry_mutex_);
120121 auto it = storage_.find (key);
121122
122123 if ( auto parent = parent_bb_.lock ())
@@ -130,10 +131,10 @@ class Blackboard
130131 auto parent_info = parent->portInfo (remapped_key);
131132 if ( parent_info )
132133 {
133- storage_.insert ( { key, Entry ( *parent_info ) } );
134+ storage_.emplace ( key, Entry ( *parent_info ) );
134135 }
135136 else {
136- storage_.insert ( { key, Entry ( PortInfo () ) } );
137+ storage_.emplace ( key, Entry ( PortInfo () ) );
137138 }
138139 }
139140 parent->set ( remapped_key, value );
@@ -195,10 +196,18 @@ class Blackboard
195196 storage_.clear ();
196197 internal_to_external_.clear ();
197198 }
199+
200+ // Lock this mutex before using get() and getAny() and unlock it while you have
201+ // done using the value.
202+ std::mutex& entryMutex ()
203+ {
204+ return entry_mutex_;
205+ }
198206
199207 private:
200208
201209 struct Entry {
210+
202211 Any value;
203212 const PortInfo port_info;
204213
@@ -213,6 +222,7 @@ class Blackboard
213222 };
214223
215224 mutable std::mutex mutex_;
225+ mutable std::mutex entry_mutex_;
216226 std::unordered_map<std::string, Entry> storage_;
217227 std::weak_ptr<Blackboard> parent_bb_;
218228 std::unordered_map<std::string,std::string> internal_to_external_;
0 commit comments