Using WaitSets and Conditions.
Using WaitSets and Conditions.
The following #includes are needed for the examples on this page
#include <iostream>
#include <dds/core/ddscore.hpp>
#include <dds/sub/ddssub.hpp>
Setting up a WaitSet
- Create a WaitSet and attach Conditions
waitset += guard_cond;
waitset += status_cond;
<<reference-type>> A condition whose trigger value is under the control of the application.
Definition: TGuardCondition.hpp:46
<<reference-type>> A condition associated with each dds::core::Entity
Definition: TStatusCondition.hpp:50
<<reference-type>> Allows an application to wait until one or more of the attached Condition objects ...
Definition: TWaitSet.hpp:56
WaitSet & attach_condition(dds::core::cond::Condition cond)
Attaches a Condition to the WaitSet.
Definition: TWaitSet.hpp:259
<<reference-type>> Condition specifically dedicated to read operations and attached to one dds::sub::...
Definition: TReadCondition.hpp:40
The DataState class describes the state of a sample and includes the information about the sample's I...
Definition: status/DataState.hpp:275
static const InstanceState any()
Creates an InstanceState object representing any instance state.
Definition: status/DataState.hpp:263
static const SampleState not_read()
Creates a not_read SampleState object.
Definition: status/DataState.hpp:92
static const ViewState any()
Creates a ViewState object representing any view state.
Definition: status/DataState.hpp:166
Waiting for Condition(s) to trigger
You have two ways to wait for Conditions to trigger: wait or dispatch.
- Wait example
for (uint32_t i = 0; i < active_conditions.size(); i++) {
if (active_conditions[i] == guard_cond) {
std::cout << "guard_cond was triggered\n";
} else if (active_conditions[i] == status_cond) {
std::cout << "status_cond was triggered\n";
}
}
static Duration from_secs(double seconds)
Create a Duration elapsing a specific number of seconds.
const ConditionSeq wait(const dds::core::Duration &timeout)
Allows an application thread to wait for the occurrence of certain conditions.
Definition: TWaitSet.hpp:139
std::vector< dds::core::cond::Condition > ConditionSeq
A vector of Conditions.
Definition: TWaitSet.hpp:61
- Dispatch example
std::cout << "read_cond1 was triggered\n";
});
ReadCondition read_cond2(
reader2,
[](Condition c) {
auto rc = dds::core::polymorphic_cast<ReadCondition>(c);
auto reader = rc.data_reader();
std::cout << "read_cond2 was triggered (topic is "
<< reader.topic_name() << ")" << std::endl;
});
waitset += read_cond1;
waitset += read_cond2;
<<reference-type>> Abstract base class of all the conditions
Definition: TCondition.hpp:58
void dispatch(const dds::core::Duration &timeout)
Waits for at least one of the attached conditions to trigger and then dispatches the events.
Definition: TWaitSet.hpp:218
static DataState any()
Create a DataState with InstanceState::any(), ViewState::any(), and SampleState::any()
Definition: status/DataState.hpp:470
- See also
- Filtering with Query Conditions