{"id":100305,"date":"2018-11-23T07:00:00","date_gmt":"2018-11-23T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=100305"},"modified":"2019-03-13T00:14:35","modified_gmt":"2019-03-13T07:14:35","slug":"20181123-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20181123-00\/?p=100305","title":{"rendered":"The default number of threads in an I\/O completion port is the number of processors, but is that logical or physical processors?"},"content":{"rendered":"<p>The <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/FileIO\/createiocompletionport\"><code>Create&shy;Io&shy;Completion&shy;Port<\/code><\/a> function lets you specify how many concurrent threads can be processing work from the completion port. But if you pass a value of 0 for <code>Number&shy;Of&shy;Concurrent&shy;Threads<\/code>, &#8220;the system allows as many concurrently running threads as there are processors in the system.&#8221; <\/p>\n<p>Are these physical processors or logical processors? <\/p>\n<p>They are logical processors. <\/p>\n<p>One way to figure this out is that the goal of the I\/O completion port is to keep CPU usage at exactly 100%. If the I\/O completion port consumed only as many threads as physical processors, then you wouldn&#8217;t quite get to 100% CPU utilization, because there would be extra capacity on the unused logical processors. <\/p>\n<p>Another way to figure this out is to use your understanding of history. I\/O completion ports were created long before hyperthreading was invented, so this code <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">treated all logical processors as full-fledged processors<\/a>. <\/p>\n<p>And a third way to figure it out is to test it. <\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;strsafe.h&gt;\n\n#define THREADS 10\n\nLONG ActiveThreads = 0;\n\nDWORD CALLBACK IoThread(void* Port)\n{\n DWORD Bytes;\n ULONG_PTR Key;\n OVERLAPPED* Overlapped;\n while (GetQueuedCompletionStatus(Port, &amp;Bytes,\n              &amp;Key, &amp;Overlapped, 1000)) {\n  TCHAR msg[64];\n  auto count = InterlockedIncrement(&amp;ActiveThreads);\n  StringCchPrintf(msg, ARRAYSIZE(msg), TEXT(\"&gt;%d\\r\\n\"), count);\n  OutputDebugString(msg);\n\n  DWORD Tick = GetTickCount();\n  while (GetTickCount() - Tick &lt; 1000) { }\n\n  count = InterlockedDecrement(&amp;ActiveThreads);\n  StringCchPrintf(msg, ARRAYSIZE(msg), TEXT(&quot;&lt;%d\\r\\n&quot;), count);\n  OutputDebugString(msg);\n }\n return 0;\n}\n\nint __cdecl main(int, char**)\n{\n HANDLE Port = CreateIoCompletionPort(INVALID_HANDLE_VALUE,\n                nullptr, 0, 0);\n\n HANDLE ThreadHandles[THREADS];\n int i;\n for (i = 0; i &lt; THREADS; i++) {\n  DWORD Id;\n  ThreadHandles[i] = CreateThread(0, 0, IoThread, Port, 0, &amp;Id);\n }\n\n for (i = 0; i &lt; THREADS * 2; i++) {\n  PostQueuedCompletionStatus(Port, 0, 0, nullptr);\n }\n\n for (i = 0; i &lt; THREADS; i++) {\n  WaitForSingleObject(ThreadHandles[i], INFINITE);\n }\n\n return 0;\n}\n<\/pre>\n<p>Pick a value for <code>THREADS<\/code> that is greater than the number of logical processors. <\/p>\n<p>We start by creating an I\/O completion port and a bunch of threads whose job it is to complete work posted to the port. we then post a lot of work to the port and wait for the threads to drain the work. <\/p>\n<p>Each thread grabs a work item, then increments a counter that lets us know how many threads are actively processing work. The thread then goes into a tight spin loop for one second. It has to do this rather than <code>Sleep<\/code> because the thread needs to be actively doing work for it to be counted against the I\/O completion port&#8217;s concurrency limit. <\/p>\n<p>After wasting some time, the thread decrements the count of active threads, and then goes back to looking for more work. <\/p>\n<p>Along the way, we print the number of active threads. <\/p>\n<p>Run this program, and you&#8217;ll see that it retires work in chunks, and the number of threads in each chunk is the number of logical processors. <\/p>\n<p>So there, confirmed by experimentation. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>How much stuff can happen at once?<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-100305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>How much stuff can happen at once?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=100305"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100305\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=100305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=100305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=100305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}