{"id":109360,"date":"2024-02-05T07:00:00","date_gmt":"2024-02-05T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109360"},"modified":"2024-02-05T06:57:14","modified_gmt":"2024-02-05T14:57:14","slug":"20240205-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240205-00\/?p=109360","title":{"rendered":"How can I close a thread pool and cancel all work that had been queued to it?"},"content":{"rendered":"<p>When you close a custom Win32 thread pool, the call to <code>Close\u00adThreadpool<\/code> returns immediately, but the actual closure doesn&#8217;t occur until all pending work has completed. What if you want to abandon any work that hasn&#8217;t yet begun?<\/p>\n<p>What you can do is put the work items in a thread pool cleanup group, and when you want to abandon all of the pending callbacks, call <code>Close\u00adThreadpool\u00adCleanup\u00adGroup\u00adMembers<\/code> and pass <code>TRUE<\/code> for <code>fCancel\u00adPending\u00adCallbacks<\/code>. The function abandons any callbacks that haven&#8217;t started but waits for for callbacks that have already started.\u00b9<\/p>\n<p>Here&#8217;s a sample, with no error checking.<\/p>\n<pre>auto pool = CreateThreadpool(0);\r\nauto group = CreateThreadpoolCleanupGroup();\r\n\r\nTP_CALLBACK_ENVIRON env;\r\nInitializeThreadpoolEnvironment(&amp;env);\r\nSetThreadpoolCallbackPool(&amp;env, pool);\r\nSetThreadpoolCallbackCleanupGroup(&amp;env, group, nullptr);\r\n\r\n\/\/ Queue up 100 slow-running tasks.\r\nauto callback = [](auto instance, auto context, auto work)\r\n    {\r\n        printf(\"%d\\n\", PtrToInt(context));\r\n        Sleep(100);\r\n    };\r\nfor (int i = 0; i &lt; 100; i++)\r\n{\r\n    auto work = CreateThreadpoolWork(callback, IntToPtr(i), &amp;env);\r\n    SubmitThreadpoolWork(work);\r\n}\r\n\r\n\/\/ Shut down the entire group.\r\n\/\/ Any callbacks that haven't started will be abandoned.\r\nCloseThreadpoolCleanupGroupMembers(group, TRUE, nullptr);\r\n\r\n\/\/ Clean up the thread pool when done.\r\nCloseThreadpool(pool);\r\n<\/pre>\n<p>\u00b9 If you want the ones that have already started to respond to the shutdown, you can create a custom signal and have each callback explicitly periodically check that custom signal themselves.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can put them in a group.<\/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-109360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can put them in a group.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109360","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=109360"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109360\/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=109360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}