dxFeed Graal CXX API v9.0.0
Loading...
Searching...
No Matches
Lifetime of the entities

The entities of the library live as in the Java API: an entity that is closed is released when the last pointer to it is released, and the library does not call its listeners after it is closed.

Owners. Releasing the last pointer of the user does not close everything:

An endpoint is not closed with its last pointer, as in the Java API, where an endpoint is not closed by the garbage collector. DXEndpoint::create()->connect(address)->getFeed() keeps no pointer to the endpoint, and the feed does not own its endpoint: closing it there would leave the feed without data and without an error. The last pointer can also be released in a listener on a thread of the library or at the exit of the process, where close() could block or fail.

AutoClose closes an entity when it goes out of scope, as try-with-resources does in Java.

Closing elsewhere. To close an entity in another scope or on another thread, keep a std::shared_ptr to it and call close() there: close() can be called from any thread and more than once. Or move the AutoClose there: return it from a function, keep it in a member of the owner or move it into a lambda.

struct Session {
dxfcpp::AutoClose<dxfcpp::DXEndpoint> endpoint; // Closed when the Session is destroyed.
};
std::thread worker{[endpoint = dxfcpp::AutoClose{dxfcpp::DXEndpoint::create()->connect(address)}] {
// ...
}}; // The endpoint is closed when the thread ends.
Session represents a continuous period of time during which apply same rules regarding trading activi...
Definition Session.hpp:35

One AutoClose must not be used from two threads at once, as one std::shared_ptr must not; moving it to another thread is fine.

close(). DXFeedSubscription::close() returns after the calls of its listeners in progress on other threads end; called from a listener of the library, it does not wait. A batch of events that the Java code delivers after close() is dropped. A subscription or an endpoint can be closed from a listener of a subscription. CLOSED is the last state notification of an endpoint. A closed subscription delivers no events; adding a listener to it, attaching it to a feed or closing it again does nothing.

DXEndpoint::close() stops the events, but it does not wait for the calls of the listeners in progress: the subscriptions of the endpoint can be released on another thread. Close the subscription whose listener uses the objects of a scope before these objects are destroyed, for example with an AutoClose declared after them.

Executor. DXEndpoint::executor() called after DXEndpoint::getFeed() applies to the next events of the existing subscriptions, as in the Java API.

Exit. At the exit of the process the library does not close the endpoints: the listeners that use the objects of the program can be called while these objects are being destroyed. Close the endpoints before that, or call dxfcpp::shutdown() (see its documentation).