Adding a std::thread-based ReadCondition to a Waitset (C++)

3 posts / 0 new
Last post
His Nerdship's picture
Offline
Last seen: 5 years 5 months ago
Joined: 05/08/2019
Posts: 18
Adding a std::thread-based ReadCondition to a Waitset (C++)

I am writing a (modern C++) module to simulate a device that connects to a central registry to send and obtain information.  The registry and device each pass data both ways, so act as both subscribers and publishers.

Both the device's publish and subscribe functions operate in their own threads. The publisher sets up a std::condition_variable, condvar, calls condvar.wait_for(nSecs), then sends its data.  When the subscriber receives the response from the registry, it calls condVar.notify_one() to notify the publisher, which then exits.

If no notification from the subscriber is received, the nSecs timeout fires, and the publisher re-sends the data.  After the third attempt it aborts the transmission.  In the meantime the subscriber waits indefinitely with waitset.dispatch();

The problem with this is that if no response is received, that subscriber thread hangs indefinitely.

The subscriber's WaitSet already has a regular ReadCondition attached (DataState::any()) to cater for successful responses from the registry. I would like to add one (or a QueryCondition) to cater for failure, based, say, on condition_variable::wait().  The publisher, after aborting the transmission, can send off a condition_variable::notify_one(), so that the WaitSet can break off and terminate the subscriber thread.  I couldn't see a way to set up a condition to do this.

This is my first DDS application, so any suggestions most welcome.

Organization:
Offline
Last seen: 1 month 3 weeks ago
Joined: 04/02/2013
Posts: 196

Hi,

I think what you need is a GuardCondition. A GuardCondition let's you manually set its trigger value to true or false, which lets you wake up the WaitSet from the publish thread.

Alex

His Nerdship's picture
Offline
Last seen: 5 years 5 months ago
Joined: 05/08/2019
Posts: 18

That worked fine.  ¡Muchas gracias Alejandro!