How can I prevent deadlocks while invoking the RTI APIs from a Listener?

Note: Applies to RTI Connext 4.2 and above

To prevent deadlocks, RTI uses mutexes to coordinate access to critical data and operations between threads. Mutexes are grouped in Exclusive Areas (EAs) code based on the regions they protect. However, deadlocks are still possible if you try to use certain RTI Connext API calls (see Section 4.5.1 in the Core Libraries and Utilities User's Manual) from the Receive Thread's listener callback. 
       
Here are some proven usage patterns that prevent such deadlocks:

  1. (Recommended solution) Create a new thread that will process incoming messages and call  read() or  take() when the Waitset triggers. This method avoids using a Listener.

  2. Configure individual Publishers and Subscribers (and thus DataWriters and DataReaders) so they share their DomainParticipant's EA. This results in a single EA shared among all Entities. Then you can create/delete/enable an Entity in a Listener callback.

Note: By having all RTI APIs protected by a single EA (and thus a single mutex), your application will be less concurrent. For more details on EAs, please see Section 4.5 in the Core Libraries and Utilities User's Manual.

Keywords: