OnixS C++ FIX Engine 4.14.0
Users' manual and API documentation
Loading...
Searching...
No Matches
FIX Engine Threading Models

The FIX Engine supports the following threading models for usual sockets:

The OnixS::FIX::ThreadingModel::ExternalThread mode supports the Solarflare TCPDirect technology that is described in the Solarflare TCPDirect section.

The model is configured via the OnixS::FIX::EngineSettings::threadingModel(ThreadingModel::Enum value) method.

DedicatedThreads

If this mode is used and there is an active connection the FIX Engine creates receiving and sending threads on each session instance. The receiving thread receives and processes incoming FIX messages. The sending thread transmits user's FIX messages which were stored in the internal sending queue. The OnixS::FIX::Session::messageGrouping parameter affects the sending process. If the parameter is zero (by default) or one, the FIX Engine tries to send a FIX message in the primary application thread and only after a failure, stores it in the sending queue for the subsequent transmission from the sending thread. If the parameter is greater than one the FIX Engine stores a serialized FIX message packet in the sending queue immediately and then transmits it from the sending thread.

The following table explains which callbacks could be called by the each one thread.

Callback Primary application thread Sending thread Receiving thread
OnixS::FIX::ISessionListener::onError Yes Yes Yes
OnixS::FIX::ISessionListener::onInboundApplicationMsg No No Yes
OnixS::FIX::ISessionListener::onInboundSessionMsg No No Yes
OnixS::FIX::ISessionListener::onOutboundApplicationMsg Yes Yes Yes
OnixS::FIX::ISessionListener::onOutboundSessionMsg Yes Yes Yes
OnixS::FIX::ISessionListener::onReceivedBytes No No Yes
OnixS::FIX::ISessionListener::onMessageSending Yes Yes Yes
OnixS::FIX::ISessionListener::onResendRequest No No Yes
OnixS::FIX::ISessionListener::onResendingStarted No No Yes
OnixS::FIX::ISessionListener::onResendingFinished No No Yes
OnixS::FIX::ISessionListener::onStateChange Yes Yes Yes
OnixS::FIX::ISessionListener::onWarning Yes Yes Yes

See Callback Safety for callback restrictions, lock-related deadlock risks, and data lifetime.

Note
Sessions with SSL encryption always use the OnixS::FIX::ThreadingModel::DedicatedThreads threading model.

ThreadPool

If this mode is used the FIX Engine creates the thread pool and shares it between all sessions. The maximum threads number in the thread pool depends on the OnixS::FIX::EngineSettings::threadPoolSize parameter. The default value (0) of this parameter means that the thread pool's size will be equal to the CPU's number. In the thread pool, each thread performs read/write operations and can process a number of different FIX connections, which are distributed equally among all threads. The thread pool is used also to perform the asynchronous TCP connect when the OnixS::FIX::Session::logonAsInitiatorAsync() method is called.

Callbacks caused by network processing, including inbound messages and resend requests, run on a pool thread. Callbacks caused directly by an application call, such as the outbound callbacks during OnixS::FIX::Session::send, run on the calling thread. State changes, errors, and warnings run in the context of the operation that reports them; do not assume they always run on a pool thread. Slow callbacks can delay other connections serviced by that thread.

ExternalThread

In OnixS::FIX::ThreadingModel::ExternalThread mode the FIX Engine does not create I/O threads. Such mode means that user creates additional threads himself (or uses the main thread), then performs event dispatching in a loop by calling the OnixS::FIX::ISessionReactor::dispatchEvents() routine. This mode supports the Solarflare's TCPDirect and regular TCP connections. For TCPDirect connections, one should use the OnixS::FIX::TCPDirect::Stack as a reactor. For regular TCP connections, one should use OnixS::FIX::TCPStandard::Stack as a reactor.

Callbacks for dispatched network events run synchronously on the thread calling OnixS::FIX::ISessionReactor::dispatchEvents. Calls such as OnixS::FIX::Session::send can also invoke callbacks directly, before returning to the application. Both dispatch and session operations must stay on the reactor-owning thread. See Deferred Processing and Shutdown for performing deferred operations after dispatch returns.

Note
The TCPStandard/TCPDirect technology is NOT thread-safe. Therefore, the event dispatching and message sending should be performed from the same thread.
Note
The OnixS::FIX::ThreadingModel::ExternalThread mode is set automatically when an external reactor is provided to the session's constructor or OnixS::FIX::Engine::init() method, regardless of which threading model was specified by the OnixS::FIX::EngineSettings::threadingModel. If the OnixS::FIX::ThreadingModel::ExternalThread mode is configured, but an external reactor is not provided during the Session's construction or FIX Engine initialization, an exception will be thrown.

Additional Threads That Can Be Spawned

If the configured threading model equals OnixS::FIX::ThreadingModel::ThreadPool or OnixS::FIX::ThreadingModel::DedicatedThreads and the ListenPort parameter is greater than zero, the FIX Engine creates the thread pool to process incoming TCP connections. The number of threads in the thread pool depends on the number of listening ports. The maximum thread number in the thread pool is also controlled by the OnixS::FIX::EngineSettings::threadPoolSize parameter.

If the configured threading model equals OnixS::FIX::ThreadingModel::DedicatedThreads and the OnixS::FIX::Session::logonAsInitiatorAsync() method is used, the FIX Engine creates a separate thread to perform the asynchronous TCP connect. Each such thread is started when the OnixS::FIX::Session::logonAsInitiatorAsync() method is called and is stopped when the asynchronous TCP connect is completed.

When a FIX session has some network issues, the FIX Engine will restore the FIX connection. If the configured threading model equals OnixS::FIX::ThreadingModel::DedicatedThreads, the FIX Engine creates a separate reconnect thread to perform the reconnect logic.

If the OnixS::FIX::SessionStorageType::AsyncFileBased value is passed to the session constructor, the FIX Engine creates one additional thread on each session instance. This thread is used to process session storage file operations in the asynchronous mode.

See also
ExternalThread Mode
Scheduler Threading Models