Use StatusConditions Instead of ReadConditions with DDS_ANY_READ_STATE | DDS_ANY_VIEW_STATE | DDS_ANY_INSTANCE_STATE

When using a WaitSet, you can be notified of new data arriving two ways:

  1. Get a DataReader’s status condition, and set the enabled statuses to include the DATA_AVAILABLE_STATUS.
  2. Use a DataReader to create a read condition

Both of these options will notify you that there is new data available.  However, there are differences in using the two options.  If you choose to use a ReadCondition, you can have finer granularity about when you are notified about data, but this comes at the expense of code complexity - especially if you are already using the StatusCondition to be notified of other events.  If you are using a ReadCondition, you have to create a new ReadCondition object and maintain a reference to it rather than using the pre-created StatusCondition object belonging to the DataReader.

If you only want to be notified that there is new data available, and you do not care about the instance state, read state, or sample state, you should use a StatusCondition.  This provides you with a notification that there is data available.

An example of using a StatusCondition to be notified that new data is available:

        StatusCondition *condition = reader->get_statuscondition();
        condition->set_enabled_statuses(DDS_DATA_AVAILABLE_STATUS);

        // ... Cast reader as helloReader, etc.
  
        WaitSet *waitSet = new WaitSet();
        waitSet->attach_condition(condition);

        // If you attach multiple conditions, this will tell you which one is true
        ConditionSeq activeConditions;
        DDS_Duration_t timeout = {1,0};

        DDS_ReturnCode_t retcode = waitSet->wait(activeConditions, timeout);

        // ... Check that this isn't a timeout, etc.
 
        // Simple example that uses only one condition.  Iterate over the active 
        // conditions in a real example.
        if (activeConditions[0] == reader->get_statuscondition()) 
        { 
            HelloMsgSeq helloMsgSeq;
            SampleInfoSeq sampleInfos;
 
            // Call read() or take() here. Note, if you do not call read(), 
            // take(), read_instance(), etc., the StatusCondition will remain 
            // true, and the WaitSet will not sleep again. 
            retcode = helloReader->read(helloMsgSeq, sampleInfos); 
        }

 

A full example of using a StatusCondition and a WaitSet to be notified that data is available is here:
http://community.rti.com/examples/waitset-status-condition

The alternative to using a StatusCondition is to use a ReadCondition.  This is useful if you are only interested in being notified about data in the queue that has a specific sample state, view state, or instance state.  For example, if you want to be notified that data is in the queue that has the instance state of NOT_ALIVE_NO_WRITERS or NOT_ALIVE_DISPOSED, you can use a ReadCondition to be notified when data in the queue has this state. 
 

An example of using a ReadCondition:

        ReadCondition* read_condition = reader->create_readcondition(  
                DDS_ANY_SAMPLE_STATE,
                DDS_ANY_VIEW_STATE,
                NOT_ALIVE_NO_WRITERS_INSTANCE_STATE |
                NOT_ALIVE_DISPOSED_INSTANCE_STATE);

        WaitSet* waitset = new WaitSet(); 
        waitset->attach_condition(read_condition);
 
        // ...

        ConditionSeq activeConditions;
        DDS_Duration_t timeout = {1,0};

        // Wait to be notified of not alive no writers/ not alive disposed data
        retcode = waitset->wait(activeConditions, timeout); 

        // ... Check that this isn't a timeout, etc.  

        // Simple example that uses only one condition.  Iterate over the active 
        // conditions in a real example.
        if (activeConditions[0] == read_condition)
        { 
            waitsetsSeq data_seq;
            SampleInfoSeq info_seq;

            // You can use take_w_condition() or read_w_condition() on 
            // the Data Reader.  This gives you the benefit of only 
            // accessing the data you were notified about
            retcode = waitsets_reader->take_w_condition(
                                             data_seq, info_seq,
                                             DDS_LENGTH_UNLIMITED,
                                             read_condition);
        }