Iocp

CreateIocp()

Create an IOCP with its own thread pool. The thread pool will use 'numThreads' threads all at the given thread priority level. Typically the number of threads should be about twice the number of processors in the system. More are needed if IOCP worker threads are made to block in application code. If numThreads == 0 then a suitable default is chosen.

'numberOfConcurrentThreads' gives the maximum number of threads that the operating system can allow to concurrently process I/O completion packets for the I/O completion port. If this parameter is zero, the system allows as many concurrently running threads as there are processors in the system.

Returns nullptr on failure. In that case a specific error code can be obtained with a call to WSAGetLastError().


Iocp* CreateIocp(ssize_t numThreads = 0);

GetIoService()

Get the associated boost asio io_service.


boost::asio::io_service& GetIoService(Iocp* iocp);

Close()

Every successful call to CreateIocp() (i.e. that doesn't return nullptr) must be paired with a call to Close(Iocp*).

The Iocp keeps track of the number of TcpClients, TcpServers and TcpMsgConnections that have been associated with it using a counter. A call to Close(Iocp*) blocks until the counter falls to zero (in order that the Iocp threads are only stopped when it is appropriate).

When for a ITcpEndpoint is closed, the Iocp counter typically isn't decremented immediately - because ITcpEndpoint::Close() is asynchronous.

Calling Close(Iocp*) without closing the TcpClients, TcpServers and TcpMsgConnections will block indefinitely. During the "shut down process" it is permissible for other threads to be closing these things after Close(Iocp*) has already been called! However there are two rules that should be adhered to avoid undefined behaviour:

These rules are sufficient to ensure that the Iocp counter only transitions to zero once.

Close(Iocp*) only returns after all the Iocp threads have been stopped


void Close(Iocp*);