Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I/O Completion Port vs. QueueUserApc?

Under Windows, there are two means to insert work items for avoiding to create too many threads:

Means 1: Use IOCP;

Means 2: Use QueueUserApc.

However, means 1 is far more intricate than means 2.

So my question is: what are the advantages of means 1 relative to that of means 2?

like image 952
xmllmx Avatar asked Jan 21 '13 19:01

xmllmx


1 Answers

When you call QueueUserApc, you must target a specific thread.

IOCP has a built-in thread dispatch mechanism that QueueUserApc lacks that allows you to target the most efficient thread out of a pool of threads. The thread dispatch mechanism automatically prevents too many threads from running at the same time (which causes extra context switches and extra contention) or too few threads from running at the same time (which causes poor performance).

Windows actually keeps track of the number of threads running IOCP jobs. It initially sets the number of threads it allows to run equal to the number of virtual cores on the machine. However, if a thread blocks for I/O or synchronization, another thread blocked on the IOCP port is automatically released, avoiding thread starvation.

In addition, IOCP can be easily hooked up to I/O so that I/O events trigger dispatches of threads blocked on the IOCP port. This is the most efficient way to do I/O to a large number of destinations on Windows.

like image 181
David Schwartz Avatar answered Sep 28 '22 01:09

David Schwartz