Skip to content

Dev_ThreadSafety

Mehmet Emre Çakal edited this page Oct 31, 2024 · 1 revision

Thread Safety in ROS# for Message Reception

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.

Subscriber : Communicator

  • Properties and Fields

    • _doEnsureThreadSafety: Determines if thread safety is enabled. If true, incoming messages are processed within a lock 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 the Subscribe method in RosSocket or directly when creating a Subscriber<T>. It configures _receiveMethod to point to either ReceiveThreadSafe or ReceiveNonThreadSafe, 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.

RosSocket.cs

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.

UnitySubscriber.cs

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.

Best Practices

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

Clone this wiki locally