@@ -63,20 +63,39 @@ pointcloud instance by reference.
63
63
64
64
The most notable issue, when using the ` shared_ptr ` approach, is that it is ** NOT thread safe** .
65
65
66
- IF multithreading is used, inside a Node (remember that BT.CPP is single-threaded by default),
67
- then there is no guarantee that a copy of the object being shared isn't accessed during writing .
66
+ If a custom asynchronous Node has its own thread, then the actual object might be accessed by other
67
+ threads at the same time .
68
68
69
69
To prevent this issue, we provide a different API that includes a locking mechanism.
70
70
71
+ First, when creating our ports we can use a plain ` Pointcloud ` , no need to wrap it inside a ` std::shared_ptr ` :
71
72
72
73
``` cpp
73
- // inside this scope (as long as any_locked exists), a mutex protecting
74
- // the instance of "cloud" remains locked
74
+ PortsList AcquirePointCloud::providedPorts ()
75
+ {
76
+ return { OutputPort<Pointcloud>("cloud") };
77
+ }
78
+
79
+ PortsList SegmentObject::providedPorts ()
80
+ {
81
+ return { InputPort<std::string>("obj_name"),
82
+ InputPort<Pointcloud>("cloud"),
83
+ OutputPort<Pose3D>("obj_pose") };
84
+ }
85
+ ```
86
+
87
+ To access the instance of Pointcloud by pointer/reference:
88
+
89
+ ``` cpp
90
+ // inside the scope below, as long as "any_locked" exists, a mutex protecting
91
+ // the instance of "cloud" will remain locked
75
92
if (auto any_locked = getLockedPortContent(" cloud" ))
76
93
{
77
94
if(any_locked->empty())
78
95
{
79
96
// the entry in the blackboard hasn't been initialized yet.
97
+ // You can initialize it doing:
98
+ any_locked.assign(my_initial_pointcloud);
80
99
}
81
100
else if(Pointcloud* cloud_ptr = any_locked->castPtr<Pointcloud >())
82
101
{
0 commit comments