
3.1. WRITING DATA 13
ts = {3, 27.0F, 75.0F, CELSIUS};
dw.write(ts);
std::this_thread::sleep_for(std::chrono::seconds(10));
//[NOTE #2]: Instances automatically unregistered and
// disposed as result of the destruction of the dw object
return 0;
}
3.1.3 Explicit Life-cycle Management
Topic-instances life-cycle can also be managed explicitly via the API de-
fined on the DataWriter. In this case the application programmer has
the control on when instances are registered, unregistered and disposed.
Topic-instances registration is a good practice to follow whenever an appli-
cations writes an instance very often and requires the lowest latency write.
In essence the act of explicitly registering an instance allows the middleware
to reserve resources as well as optimize the instance lookup. Topic-instance
unregistration provides a mean for telling DDS that an application is done
with writing a specific topic-instance, thus all the resources locally associ-
ated with can be safely released. Finally, disposing topic-instances gives a
way of communicating DDS that the instance is no more relevant for the
distributed system, thus whenever possible resources allocated with the spe-
cific instances should be re leased both locally and remotely. Listing 2 shows
and example of how the DataWriter API can be used to register, unreg-
ister and dispose topic-instances. I n order to show you the full life-cycle
management I’ve changed the default DataWriter behavior to avoid that
instances are automatically disposed when unregistered. In addition, to
maintain the code compact I take advantage of the new C++11 auto fea-
ture which leaves it to the the compiler to infer the left hand side types from
the right hand side return-type. Listing 2 shows an application that writes
four samples belonging to four different topic-instances, respectively those
with id = 0, 1, 2, 3. The instances with id = 1, 2, 3 are explicitly registered
by calling the DataWriter::register
instance method, while the in-
stance with id = 0 is automatically registered as result of the write on the
DataWriter. To show the different possible state transitions, the topic-
instance with id = 1 is explicitly unregistered thus causing it to transition
to the NOT ALIVE NO WRITER state; the topic-instance with id = 2 is ex-
plicitly disposed thus causing it to transition to the NOT
ALIVE DISPOSED
state. Finally, the topic-instance with id = 0, 3 will be automatically unreg-
istered, as a result of the destruction of the objects dw and dwi3 respectively,
thus transitioning to the state NOT
ALIVE NO WRITER. Once again, as men-
tioned above, in this example the writer has been configured to ensure that
topic-instances are not automatically disposed upon unregistration.