7.2. Threading model¶
Operations on the same
Connector instance or any contained
Output are, in general, not protected for multi-threaded access. The only
exceptions are the following wait operations.
- Thread-safe operations:
Connector.wait()(wait for data on any
Output.wait()(wait for acknowledgments)
Input.wait()(wait for data on this
These operations can block a thread while the same
Connector is used in
a different thread.
both be called at the same time on the same
Output.write() can block the current thread under certain
Output.write() is not thread-safe.
All operations on different
Connector instances are thread-safe.
Applications can implement their own thread-safety mechanism around a
instance. The following section provides an example.
7.2.1. Protecting calls to Connector¶
This example shows how to use the Python
threading package to
protect calls to the same
import threading connector = rti.Connector("MyParticipantLibrary::MyParticipant", "ShapeExample.xml") lock = threading.RLock() def read_thread(): with lock: # Protect access to methods on the same Connector input = connector.get_input("MySubscriber::MySquareReader") input.wait() # wait outside the lock with lock: # Take the lock again input.take(); for sample in input.samples.valid_data_iter: print(sample.get_dictionary()) def write_thread(): with lock: # Protect access to methods on the same Connector output = connector.get_output("MyPublisher::MySquareWriter") output.instance['x'] = 10 output.write() # Spawn read_thread and write_thread...