Skip to content

Commit deb687c

Browse files
committed
tutorial modified
1 parent 54f276a commit deb687c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

docs/guides/blackboard_reference.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sidebar_position: 4
33
---
44

5-
# Access Blackboard objects by Reference
5+
# Zero-copy access to the blackboard, using pointers
66

77
If you followed the tutorials, you should already know that the Blackboard uses **value semantic**, i.e.
88
the methods `getInput` and `setOutput` copy the value from/to the blackboard.
@@ -18,7 +18,7 @@ For instance, a Node where it is recommended to use reference semantic is the
1818
`LoopNode` decorator, that modifies "in-place" a vector of objects.
1919

2020

21-
## Blackboard entries as Share pointers
21+
## Method 1: Blackboard entries as Share pointers
2222

2323
For the sake of simplicity, we will consider an object that is expensive to copy,
2424
called **Pointcloud**.
@@ -59,16 +59,15 @@ The methods `getInput` and `setOutput` can be used as usual and still have value
5959
But since the object being copied is a `shared_ptr` we are actually accessing the
6060
pointcloud instance by reference.
6161

62-
## Thread safety
62+
## Method 2: thread-safe castPtr (recommended since version 4.5.1)
6363

64-
The most notable issue, when using this approach, is that it is **NOT thread safe**.
64+
The most notable issue, when using the `shared_ptr` approach, is that it is **NOT thread safe**.
6565

6666
IF multithreading is used, inside a Node (remember that BT.CPP is single-threaded by default),
6767
then there is no guarantee that a copy of the object being shared isn't accessed during writing.
6868

6969
To prevent this issue, we provide a different API that includes a locking mechanism.
7070

71-
7271
This is **NOT** thread-safe:
7372

7473
```cpp
@@ -80,11 +79,19 @@ getInput("cloud", cloud_ptr);
8079
The thread-safe alternative:
8180
8281
```cpp
83-
// inside this scope, the mutex protecting the instance of "pointcloud" remains locked
82+
// inside this scope (as long as any_locked exists), a mutex protecting
83+
// the instance of "cloud" remains locked
8484
if(auto any_locked = getLockedPortContent("cloud"))
8585
{
86-
auto cloud_ptr = any_locked.get()->cast<std::shared_ptr<Pointcloud>>();
87-
// modify the pointcloud referenced by cloud_ptr here
86+
if(any_locked->empty())
87+
{
88+
// the entry in the blackboard hasn't been initialized yet.
89+
}
90+
else if(auto cloud_ptr = any_locked->cast<std::shared_ptr<Pointcloud>>())
91+
{
92+
// Succesful cast to the original value.
93+
// Modify the pointcloud, referenced by cloud_ptr, here
94+
}
8895
}
8996
```
9097

docusaurus.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const config = {
4545
lastVersion: 'current',
4646
versions: {
4747
current: {
48-
label: '4.4',
48+
label: '4.5',
4949
},
5050
3.8: {
5151
label: '3.8',

0 commit comments

Comments
 (0)