TIP 1: Use Winsock2 IOCP-capable functions, such as WSASend and WSARecv, over Win32 file I / O functions, such as WriteFile and ReadFile Tip 1: make use WSASend and WSARecv data transceiver Socket handles from Microsoft-based protocol providers are IFS handles so you can use Win32 file I / O calls with the handle. However, the interactions between the provider and file system involve many kernel / user mode transition, thread context switches, and parameter marshals that result in a significant performance penalty. you should use only Winsock2 IOCP- capable functions with IOCP The additional parameter marshals and mode transitions in ReadFile and WriteFile only occur if the provider does not have XP1_IFS_HANDLES bit set in dwServiceFlags1 of its WSAPROTOCOL_INFO structure NOTE:.. These providers have an unavoidable additional mode transition Even in the case of wsasend and wsarecv, although, Alth Readfile and WRITEFILE WILL HAVE MORE Of THEM. TIP 2: Choose The Number of The Concurrent Worker Threads Allowed and The Tot al number of the worker threads to spawn Tip 2: determining an appropriate number of worker threads The number of worker threads and the number of concurrent threads that the IOCP uses are not the same thing You can decide to have a maximum of 2 concurrent. threads used by the IOCP and a pool of 10 worker threads. You have a pool of worker threads greater than or equal to the number of concurrent threads used by the IOCP so that a worker thread handling a dequeued completion packet can call one of the Win32 "wait" functions without delaying the handling of other queued I / O packets. If there are completion packets waiting to be dequeued, the system will wake up another worker thread. Eventually, the first thread satisfies it '
s Wait and it can be run again. When this happens, the number of the threads that can be run is higher than the concurrency allowed on the IOCP (for example, NumberOfConcurrentThreads). However, when next worker thread calls GetQueueCompletionStatus and enters wait status , the system does not wake it up. in other words, the system tries to keep your requested number of concurrent worker threads. Typically, you only need one concurrent worker thread per CPU for IOCP. to do this, enter 0 for NumberOfConcurrentThreads in the CreateiocompletionPort Call when You First Create The IOCP. TIP 3: Associate A Posted I / O Operation With A Dequeued Completion Packet. Tip 3: Perform IO operations according to the completion package of the queue. GetQueuedCompletionStatus returns a completion key and an overlapped structure for the I / O when dequeuing a completion packet. You should use these two structures to return per handle and per I / O operation information, respectively. You can use your socket handle as the completion key when you register the socket with the IOCP to provide per handle information. to provide per I / O operation "extend" the overlapped structure to contain your application-specific I / O-state information. Also, make sure you provide a unique overlapped structure For Each Overlapped I / O. When An I / O Completes, The Same Pointer To The overlapped I / O Structure IS Returned. TIP 4: I / O Completion Packet Queuing Behavior. Tip 4: Complete the unusual processing of the package.
The order in which I / O completion packets are queued in the IOCP is not necessarily the same order the Winsock2 I / O calls were made. Additionally, if a Winsock2 I / O call returns SUCCESS or IO_PENDING, it is guaranteed that a completion packet will be queued to the IOCP when the I / O completes, regardless of whether the socket handle is closed. After you close a socket handle, future calls to WSASend, WSASendTo, WSARecv, or WSARecvFrom will fail with a return code other than SUCCESS or IO_PENDING, which will not generate a completion packet. The status of the completion packet retrieved by GetQueuedCompletionStatus for I / O previously posted could indicate a failure in this case. If you delete the IOCP itself, no more I / O can be posted to the IOCP because the IOCP handle itself is invalid However, the system's underlying IOCP kernel structures do not go away until all successfully posted I / Os are completed TIP 5:.. IOCP cleanup Tip 5: completion of the port release.