I'm currently searching the internet for a custom thread pool implementation. I found an implementation which uses IOCP's. I'm wondering what the benefit is, of using them? Do they provide work stealing, or something like that, I could really find an answer...
-
1Maybe you could add the link to the custom thread pool implementation you found? – Milan Gardian Jan 13 '09 at 17:51
2 Answers
IOCP = "IO Completion Port". It is a kernel object built into the Windows OS that is there to give you an intelligent way to manage multithreaded asynchronous IO.
In very simplistic (and a little over-simplified) terms, you tell the IOCP about the IO jobs you want done. It will perform them asynchronously and maintain a queue of the results of each of those jobs. Your call to tell the IOCP about the job returns immediately (it does not block while the IO happens). You are returned an object that is conceptually like the .NET IAsyncResult ... it lets you block if you choose to, or you can provide a callback, or you can periodically poll to see if the job is complete.
While doing those jobs, the IOCP uses a thread pool. The thread pool tries to limit the number of threads to the number of processors or less (that's configurable, but the intent and the default is to limit it to the number of processors). However, the IOCP makes a provision for the fact that sometimes, the tasks on these threads might block. Asycn IO tasks won't block, but you might have provided it some other kind of task. So, you can give the IOCP another number ... this is a number of threads above the "usual maximum", which the IOCP is allowed to go up to due to one of the other threads being blocked. The goal is to have up to the "usual maximum" of threads that are actually doing something (ie, not blocked). If this happens, then for a while the IOCP will be using more threads than the usual max, but it will refuse to make any new threads until it can get back down to the "usual max".
This is a brief summary, conceptual only, and as I said, over-simplified in some ways. But it should give you the general idea. Jeffrey Richter's books on the Windows OS cover this in detail (but these books are out of print now). You can find these books used, and they actually cost more used than they did originally. I think "Advanced Windows" is the title you'd want, but there may have been an updated version of the book with a different title.

- 17,338
- 10
- 71
- 88
-
1Charlie, The Richter book has been reprinted and renamed, it's now known as Windows Via C/C++. – Len Holgate Jan 13 '11 at 14:00
the best benefit using IOCP for a thread pool is, it monitors its threads and if a thread blocks more than 100ms for some reason (including page fault, blocking call etc) it realeses another thread which was waiting due to concurrency limit reached. i dont know internal implementation but i dont think it uses work stealing queue.

- 957
- 6
- 11
-
Are you sure about 100ms? Seems like a lot of time - why not an instant release especially if the number of unblocked threads are not that high compared to the max number of allowed ones. – Dmitry Sychov Jan 08 '20 at 20:52