-
Notifications
You must be signed in to change notification settings - Fork 374
Dev_ThreadSafety
This guide details the implementation of thread-safe message reception in the Subscriber<T>
class. This feature is primarily intended to avoid data corruption and potential race conditions when messages are received concurrently.
-
Properties and Fields
-
_doEnsureThreadSafety
: Determines if thread safety is enabled. Iftrue
, incoming messages are processed within alock
block to prevent concurrent access issues. -
_lock
: Control access when_doEnsureThreadSafety
is enabled. -
_receiveMethod
: A delegate that points to either a thread-safe or non-thread-safe receive method, based on_doEnsureThreadSafety
.
-
-
Constructor and Initialization
-
DoEnsureThreadSafety
is set through theSubscribe
method inRosSocket
or directly when creating aSubscriber<T>
. It configures_receiveMethod
to point to eitherReceiveThreadSafe
orReceiveNonThreadSafe
, optimizing runtime execution depending on the desired level of thread safety.
-
-
Receiving Messages
-
ReceiveThreadSafe
: If_doEnsureThreadSafety
is true, this method is used to handle messages. -
ReceiveNonThreadSafe
: Used when_doEnsureThreadSafety
is false. Messages are processed without locking, reducing synchronization overhead when thread safety is not a concern.
-
In RosSocket
, the Subscribe
method has been modified to accept an ensureThreadSafety
parameter, which determines if the subscriber should handle messages in a thread-safe manner.
var subscriber = new Subscriber<T>(id, topic, subscriptionHandler, out subscription, throttle_rate, queue_length, fragment_size, compression)
{
DoEnsureThreadSafety = ensureThreadSafety
};
This parameter is then passed to the Subscriber<T>
constructor, setting DoEnsureThreadSafety
and configuring the _receiveMethod
accordingly.
In UnitySubscriber
, when subscribing to a topic, you can now specify the EnsureThreadSafety
parameter:
rosConnector.RosSocket.Subscribe<T>(Topic, ReceiveMessage, (int)(TimeStep * 1000), ensureThreadSafety: EnsureThreadSafety);
This allows Unity projects to leverage the optional thread-safety feature directly from the subscription level.
Enable thread safety (ensureThreadSafety: true
) only when high volumes of messages are expected or when your application design involves complex threading. For most applications, especially with low-frequency updates, disabling thread safety will yield better performance without risking data integrity. ocking introduces synchronization overhead, particularly noticeable when messages are received at a high rate. This can lead to slower processing times as threads wait for access.
Please have a look at RosSocketConsoleExample.cs (SimulateParallelMessageReception). Note that accessing the subscriber itself from the rosSocket isn't a good practice and is only for demonstration purposes.
© Siemens AG, 2017-2024
-
- 1.3.1 R2D2 Setup
- 1.3.2 Gazebo Setup on VM
- 1.3.3 TurtleBot Setup (Optional for ROS2)
-
- 2.1 Quick Start
- 2.2 Transfer a URDF from ROS to Unity
- 2.3 Transfer a URDF from Unity to ROS
- 2.4 Unity Simulation Scene Example
- 2.5 Gazebo Simulation Scene Example
- 2.6 Fibonacci Action Client
- 2.7 Fibonacci Action Server
- 3.1 Import a URDF on Windows
- 3.2 Create, Modify and Export a URDF Model
- 3.3 Animate a Robot Model in Unity
- Message Handling: Readers & Writers
- Thread Safety for Message Reception
- File Server Package
- ROS-Unity Coordinate System Conversions
- Post Build Events
- Preprocessor Directives in ROS#
- Adding New Message Types
- RosBridgeClient Protocols
- RosBridgeClient Serializers
- Action Server State Machine Model
© Siemens AG, 2017-2024