- 
                Notifications
    You must be signed in to change notification settings 
- Fork 392
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 alockblock to prevent concurrent access issues.
- 
_lock: Control access when_doEnsureThreadSafetyis enabled.
- 
_receiveMethod: A delegate that points to either a thread-safe or non-thread-safe receive method, based on_doEnsureThreadSafety.
 
- 
- 
Constructor and Initialization - 
DoEnsureThreadSafetyis set through theSubscribemethod inRosSocketor directly when creating aSubscriber<T>. It configures_receiveMethodto point to eitherReceiveThreadSafeorReceiveNonThreadSafe, optimizing runtime execution depending on the desired level of thread safety.
 
- 
- 
Receiving Messages - 
ReceiveThreadSafe: If_doEnsureThreadSafetyis true, this method is used to handle messages.
- 
ReceiveNonThreadSafe: Used when_doEnsureThreadSafetyis 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-2025
- 
- 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
- 4.1 Introduction to RosBridgeClient
- 4.2 Image Publication
- 4.3 URDF Transfer
- 4.4 Fibonacci Action Client/Server
- 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
- Actions in ROS#
- Action Server State Machine Model
© Siemens AG, 2017-2025