<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Hari's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/harip/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/harip/feed.xml</link>
    <description>A blog on Windows Kernel related technologies.</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:21:39 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Windows 10 on ARM</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/windows-10-on-arm</link>
      <pubDate>Wed, 24 May 2017 12:57:32 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/?p=135</guid>
      <description><![CDATA[Windows 10 on ARM

This is the video we did at //Build for our latest project. This project has...]]></description>
      <content:encoded><![CDATA[<strong>Windows 10 on ARM</strong>

This is the video we did at //Build for our latest project. This project has been in the works for some time; it was first revealed at WinHEC in Shenzhen in December 2016 and now we are providing more technical details with a demo.

<a href="https://channel9.msdn.com/Events/Build/2017/P4171">WIndows 10 on ARM</a>

Enjoy!]]></content:encoded>
    </item>
    <item>
      <title>Running in the Background Using Background Tasks</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/running-in-the-background-using-background-tasks</link>
      <pubDate>Tue, 02 Jul 2013 14:53:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2013/07/02/running-in-the-background-using-background-tasks/</guid>
      <description><![CDATA[At Build 2013, I did a talk on background tasks, its best&nbsp;practices and new features in Windows...]]></description>
      <content:encoded><![CDATA[<p>At <a title="https://channel9.msdn.com/Events/Build/2013" href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/Build 2013">Build 2013</a>, I did a talk on background tasks, its best&nbsp;practices and new features in Windows 8.1. It was great to meet with developers&nbsp;who use background tasks to build products and learn about their experiences.</p>
<p>Here is a link to the official channel 9 recording of the talk:&nbsp;&nbsp;</p>
<ul>
<li><a href="https://channel9.msdn.com/Events/Build/2013/3-108">https://channel9.msdn.com/Events/Build/2013/3-108</a>&nbsp;&nbsp;&nbsp;</li>
<li><em><strong>Abstract:</strong> The apps that run on a device are one of the biggest factors that determine its battery<br />life. With this in mind, we designed the Windows app lifecycle for maximum<br />battery life. But that doesn&rsquo;t mean the app model precludes background<br />activities. This talk explains the background scenarios supported in Windows<br />and shows you how to write background tasks. It also covers best practices for<br />authoring background tasks so that the connected standby lifetime of the device<br />doesn&rsquo;t suffer.</em></li>
</ul>
<p>Enjoy.</p>]]></content:encoded>
    </item>
    <item>
      <title>Background model for Metro style apps</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/background-model-for-metro-style-apps</link>
      <pubDate>Thu, 24 May 2012 15:17:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2012/05/24/background-model-for-metro-style-apps/</guid>
      <description><![CDATA[It&rsquo;s been a busy few months and I haven&rsquo;t had a chance to blog much about my work or...]]></description>
      <content:encoded><![CDATA[<p>It&rsquo;s been a busy few months and I haven&rsquo;t had a chance to blog much about my work or kernel technologies. These are a couple of posts, I wrote for the appdev blog on the new background model in Windows 8 for Metro style apps. They talk about background tasks, the suspend model and how to be productive in the background.</p>
<p></p>
<ul>
<li><a href="http://blogs.msdn.com/b/windowsappdev/archive/2012/05/16/being-productive-when-your-app-is-offscreen.aspx">Being productive when your app is offscreen</a></li>
<li><a href="http://blogs.msdn.com/b/windowsappdev/archive/2012/05/24/being-productive-in-the-background-background-tasks.aspx">Being productive in the background &ndash; background tasks</a></li>
</ul>
<p>Enjoy.</p>]]></content:encoded>
    </item>
    <item>
      <title>Performance Tips for DllMains – contd.</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/performance-tips-for-dllmains-contd</link>
      <pubDate>Thu, 16 Dec 2010 09:26:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/12/16/performance-tips-for-dllmains-contd/</guid>
      <description><![CDATA[Continuing our series on DLLs and their optional entry point DllMain, this is going to be a simple...]]></description>
      <content:encoded><![CDATA[<p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><span style="mso-ansi-language: en" lang="EN"><font face="Calibri"><font style="font-size: 11pt" color="#000000">Continuing our series on DLLs and their optional entry point DllMain, this is going to be a simple tip which allows your process to exit quickly without doing unnecessary work. When a process is cleanly exiting, each DLL loaded in the process memory receives a DLL_PROCESS_DETACH callout. This callout is also sent to a DLL when that DLL is being freed (either by FreeLibrary or it’s a dependency of a DLL which is being freed and no other DLL references it). </font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><span style="mso-ansi-language: en" lang="EN"><font face="Calibri"><font style="font-size: 11pt" color="#000000">So how does a programmer distinguish between being because the process is exiting or because the DLL is being freed? Actually before we go there, does it even matter to distinguish between these two cases? The answer is yes, it matters a whole lot. When the process is exiting and your DLL receives the DLL_PROCESS_DETACH callout, the other threads in the process might have already exited, so if you try to do some synchronization here that might not work. Or think of the situation where you have created a ginormous memory block in your DLL_PROCESS_ATTACH and free it in your DETACH callout. The process is already exiting; technically you don’t need to clean up your resources because the OS will do it for you, so why do unnecessary work? Note: Heap operations are considered suspect in DllMains because of the process-wide LoaderLock around DllMains. </font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><span style="mso-ansi-language: en" lang="EN"><font face="Calibri"><font style="font-size: 11pt" color="#000000">With that said going back to the original question, how do you distinguish between process exit and FreeLibrary? This is mentioned in the DllMain documentation but its not very prominent so I am including the relevant parts here: </font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin-left: 0in; margin-right: 0in"><font face="Times New Roman"><b><span style="mso-ansi-language: en" lang="EN"><font color="#000000"><font style="font-size: 12pt">From MSDN: </font></font></span></b><span style="mso-ansi-language: en; mso-bidi-font-weight: bold" lang="EN"><a style="cursor: auto" href="http://msdn.microsoft.com/en-us/library/ms682583(VS.85).aspx"><font style="font-size: 12pt" color="#800080"><u>http://msdn.microsoft.com/en-us/library/ms682583(VS.85).aspx</u></font></a></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin-left: 0in; margin-right: 0in"><span style="font-family: "><font color="#000000"><font style="font-size: 9.5pt">When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap memory only if the DLL is being unloaded dynamically (the <i>lpReserved</i> parameter is NULL). If the process is terminating (the <i>lpvReserved</i> parameter is non-NULL), all threads in the process except the current thread either have exited already or have been explicitly terminated by a call to the </font></font><font style="font-size: 9.5pt"><a style="cursor: auto" href="http://msdn.microsoft.com/en-us/library/ms682658(v=VS.85).aspx"><b><span style="text-underline: none"><font color="#1364c4">ExitProcess</font></span></b></a><font color="#000000"> function, which might leave some process resources such as heaps in an inconsistent state. In this case, it is not safe for the DLL to clean up the resources. Instead, the DLL should allow the operating system to reclaim the memory.</font></font></span><b><span style="mso-ansi-language: en" lang="EN"></span></b></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font color="#000000"><b><span style="mso-ansi-language: en" lang="EN"><font style="font-size: 11pt">Example Usage:</font></span></b><span style="mso-ansi-language: en" lang="EN"><font style="font-size: 11pt"> </font></span></font></font></p>  <div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; line-height: normal; list-style-type: disc; padding-left: 4pt; padding-right: 4pt; background: #ddd9c3; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230">   <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">BOOL WINAPI </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">DllMain(HINSTANCE hinst, DWORD reason, VOID *reserved) </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">{ </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160; switch (reason) </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160; { </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; case DLL_PROCESS_DETACH: </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (reserved != NULL) </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font color="#000000"><font style="font-size: 9pt">&#160;<span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><span><font style="font-size: 9pt" color="#00b050">// Process Exiting – Do Nothing</font></span></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">return false; </font></font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">} </font><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;</font></span></font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt" color="#00b050">// FreeLibrary Case</font></span></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font style="font-size: 9pt" color="#00b050">// Other code </font></font></span></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; margin: 0in 0in 0pt; padding-left: 0in; padding-right: 0in; background: #ddd9c3; border-top: medium none; border-right: medium none; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-background-themecolor: background2; mso-background-themeshade: 230; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNoSpacing"><span style="font-family: ; mso-ansi-language: en" lang="EN"><font face="Consolas"><font color="#000000"><font style="font-size: 9pt">}&#160;&#160; </font></font><span><font style="font-size: 9pt" color="#00b050">// End of DllMain </font></span></font></span></p> </div>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><span style="mso-ansi-language: en" lang="EN"><font face="Calibri"><font style="font-size: 11pt" color="#000000"></font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><span style="mso-ansi-language: en" lang="EN"><font face="Calibri"><font style="font-size: 11pt" color="#000000">And that’s how easy it is to detect the process is exiting in your DllMain. Now if you ever close a process and you see that Windows is taking some time to close it, you know what the DLLs in the process are not doing. </font></font></span></p>]]></content:encoded>
    </item>
    <item>
      <title>Performance Tips for DllMains</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/performance-tips-for-dllmains</link>
      <pubDate>Thu, 04 Nov 2010 11:29:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/11/04/performance-tips-for-dllmains/</guid>
      <description><![CDATA[The DllMain is an optional entry point into a dynamic link library (DLL) which is called by the...]]></description>
      <content:encoded><![CDATA[<div sizset="0" sizcache="3" class="WordSection1">
<p class="MsoNormal">The <a href="http://msdn.microsoft.com/en-us/library/ms682583(VS.85).aspx">DllMain</a> is an optional entry point into a dynamic link library (DLL) which is called by the system when the DLL is first loaded into a process or when it is unloaded from a process. Think of it as a constructor and destructor of the DLL. In addition, each DLL&rsquo;s entry point is also called when the system starts or terminates a thread within a process with the appropriate reason. This tip is with regards to the thread attach callouts. </p>
<p class="MsoNormal">From MSDN documentation: </p>
<table cellpadding="0" cellspacing="0" border="1" class="MsoTableGrid" style="width: 100%; border-collapse: collapse; border-style: none;">
<tbody>
<tr>
<td valign="top" style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0in">
<p class="MsoNormal" style="line-height: 140%; margin: 0in 0.75pt 0pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">DLL_THREAD_ATTACH</span></p>
<p class="MsoNormal" style="line-height: 140%; margin: 0in 0.75pt 0pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">2</span></p>
</td>
<td valign="top" style="border-bottom: windowtext 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; border-left-color: windowtext; border-top: windowtext 1pt solid; border-left-width: 1pt; border-right: windowtext 1pt solid; padding-top: 0in">
<p class="MsoNormal" style="line-height: 140%; margin-left: 0.75pt; margin-right: 0.75pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">The current process is creating a new thread. When this occurs, the system calls the entry-point function of all DLLs currently attached to the process. The call is made in the context of the new thread. DLLs can use this opportunity to initialize a TLS slot for the thread. A thread calling the DLL entry-point function with DLL_PROCESS_ATTACH does not call the DLL entry-point function with DLL_THREAD_ATTACH. </span></p>
<p class="MsoNormal" style="line-height: 140%; margin-left: 0.75pt; margin-right: 0.75pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">Note that a DLL's entry-point function is called with this value only by threads created after the DLL is loaded by the process. When a DLL is loaded using <a href="http://msdn.microsoft.com/en-us/library/ms684175(v=VS.85).aspx"><b><span style="color: #1364c4; text-decoration: none">LoadLibrary</span></b></a>, existing threads do not call the entry-point function of the newly loaded DLL.</span></p>
</td>
</tr>
<tr>
<td valign="top" style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; border-top-color: windowtext; padding-left: 5.4pt; padding-right: 5.4pt; border-top-width: 1pt; border-right: windowtext 1pt solid; padding-top: 0in">
<p class="MsoNormal" style="line-height: 140%; margin: 0in 0.75pt 0pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">DLL_THREAD_DETACH</span></p>
<p class="MsoNormal" style="line-height: 140%; margin: 0in 0.75pt 0pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">3</span></p>
</td>
<td valign="top" style="border-bottom: windowtext 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; padding-right: 5.4pt; border-top-style: none; border-left-style: none; border-right: windowtext 1pt solid; padding-top: 0in">
<p class="MsoNormal" style="line-height: 140%; margin-left: 0.75pt; margin-right: 0.75pt"><span style="line-height: 140%; font-family: 'Segoe UI','sans-serif'; color: black; font-size: 9.5pt">A thread is exiting cleanly. If the DLL has stored a pointer to allocated memory in a TLS slot, it should use this opportunity to free the memory. The system calls the entry-point function of all currently loaded DLLs with this value. The call is made in the context of the exiting thread.</span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Let&rsquo;s examine the default case where all DLLs receive these thread attach callouts: </p>
<ul>
<li>
<div class="MsoListParagraphCxSpFirst" style="text-indent: -0.25in">Let&rsquo;s say you have a process with a 50 DLLs loaded in its memory and the process creates a new thread. In the default case, the system will have to send the DLL_THREAD_ATTACH callout to each of the 50 DLLs. </div>
</li>
<li>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in">It&rsquo;s possible that for some of those DLLs, their DllMain code has been paged out from memory. So, when a thread is created you would now have to pay the cost to page in their DllMain code. </div>
</li>
<li>
<div class="MsoListParagraphCxSpLast" style="text-indent: -0.25in">And if it turns out that your DllMain is not interested in the DLL_THREAD_* reason and falls through the switch statement, then it was called unnecessarily. </div>
</li>
</ul>
<p class="MsoNormal">This is an unnecessary overhead. I have seen only a few instances of DLLs having a genuine reason to track when a thread is created in the process space. Majority of the DLLs don&rsquo;t need to know when a thread is created or terminated in the process. Also, note the remark in the above table for DLL_THREAD_ATTACH, you will not receive the callout for any threads created before the DLL is dynamically loaded. This implies you will not be able to accurately track the number of threads in a process using this technique.&nbsp; </p>
<h2>DisableThreadLibraryCalls</h2>
<p class="MsoNormal">To avoid receiving these thread attach/detach callouts for your DLLs, you can call DisableThreadLibraryCalls in your DllMain. </p>
<p class="MsoNormal"><a href="http://msdn.microsoft.com/en-us/library/ms682579(VS.85).aspx">http://msdn.microsoft.com/en-us/library/ms682579(VS.85).aspx</a></p>
<p class="MsoNormal"><b>Example Usage:</b></p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">BOOL WINAPI</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">DllMain(HINSTANCE hinst, DWORD reason, VOID *reserved)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp; switch (reason) </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case DLL_PROCESS_ATTACH:</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DisableThreadLibraryCalls(hinst);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #00b050">// other code </span></span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp; <span style="color: #00b050">// Other code</span></span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">}&nbsp;&nbsp; <span style="color: #00b050">// End of DllMain </span></span></p>
</div>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">And that&rsquo;s it, your DLL will no longer will receive the thread attach/detach callouts. </p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Win32 Threadpool Tutorial Recap</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/win32-threadpool-tutorial-recap</link>
      <pubDate>Fri, 15 Oct 2010 12:33:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/10/15/win32-threadpool-tutorial-recap/</guid>
      <description><![CDATA[Download source code from MSDN Code Gallery.   This is a quick recap of all the posts in the...]]></description>
      <content:encoded><![CDATA[<p style="background: #ddd9c3" class="MsoNormal"><a href="http://code.msdn.microsoft.com/win32threadpool/">Download</a> source code from MSDN Code Gallery. </p>  <h2 class="WordSection1"><span style="line-height: 115%; font-family: &quot;Calibri&quot;,&quot;sans-serif&quot;; color: windowtext; font-size: 11pt; font-weight: normal">This is a quick recap of all the posts in the threadpool series and the methods supported by the wrapper classes. </span></h2>  <p class="MsoNormal">&#160;</p>  <ul>   <li>     <div style="text-indent: -0.25in" class="MsoListParagraphCxSpFirst"><a href="http://blogs.msdn.com/b/harip/archive/2010/10/11/introduction-to-the-windows-threadpool-part-1.aspx">Introduction to the Windows Threadpool (Part 1)</a></div>   </li>    <li>     <div style="text-indent: -0.25in" class="MsoListParagraphCxSpMiddle"><a href="http://blogs.msdn.com/b/harip/archive/2010/10/11/threadpool-work-item-priority.aspx">Threadpool Work item priority</a></div>   </li>    <li>     <div style="text-indent: -0.25in" class="MsoListParagraphCxSpMiddle"><a href="http://blogs.msdn.com/b/harip/archive/2010/10/12/introduction-to-the-windows-threadpool-part-2.aspx">Introduction to the Windows Threadpool (part 2)</a></div>   </li>    <li>     <div style="text-indent: -0.25in" class="MsoListParagraphCxSpMiddle"><a href="http://blogs.msdn.com/b/harip/archive/2010/10/13/threadpool-timer-functions.aspx">Threadpool Timer Functions</a></div>   </li>    <li>     <div style="text-indent: -0.25in" class="MsoListParagraphCxSpLast"><a href="http://blogs.msdn.com/b/harip/archive/2010/10/14/waiting-on-events-from-within-threadpool.aspx">Waiting on Events from within Threadpool</a></div>   </li> </ul>  <p class="MsoNormal">&#160;</p>  <h2 class="WordSection1">Class windowsthreadpool::SimpleThreadPool </h2>  <div class="WordSection1">   <table style="border-bottom-style: none; border-right-style: none; border-collapse: collapse; border-top-style: none; border-left-style: none" class="MsoTableLightListAccent5" border="1" cellspacing="0" cellpadding="0"><tbody>       <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; background: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p style="text-align: center" class="MsoNoSpacing" align="center"><b><span style="color: white">Public Member Functions</span></b></p>         </td>          <td style="border-bottom-color: #4bacc6; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; background: #4bacc6; border-bottom-width: 1pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p style="text-align: center" class="MsoNoSpacing" align="center"><b><span style="color: white">Description</span></b></p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItem (Function cb)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to run Function cb. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItem (Function cb, PVOID State)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to run Function cb which accepts a PVOID as parameter. </p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithLowPri (Function cb)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a low priority work item to the process-wide threadpool to run Function cb.</p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithHiPri (Function cb)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a high priority work item to the process-wide threadpool to run Function cb.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithLowPri (Function cb, PVOID State)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a low priority work item to the process-wide threadpool to run Function cb which accepts a PVOID as parameter.</p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithHiPri (Function cb, PVOID State)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a high priority work item to the process-wide threadpool to run Function cb which accepts a PVOID as parameter.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>void&#160; WaitForAll () </b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Wait for all queued work items to finish execution. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>void&#160; WaitForAllCurrentlyRunning ()</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Wait for all running work items to finish execution and cancel any queued work items which haven’t started running. </p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />PVOID&#160; RegisterEvent (Function cb, HANDLE Event, int timeout)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to call Function cb when the event is signaled or the timeout expires. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />PVOID&#160; RegisterEvent (Function cb, PVOID State, HANDLE Event, int timeout)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to call Function cb which accepts a PVOID as parameter when the event is signaled or the timeout expires.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; DestroyEvent (PVOID EventHandle)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Cancel any interest in the event previously registered. Method accepts the handle returned by the RegisterEvent method. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; ReRegisterEvent (PVOID EventHandle)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">ReRegister interest in the event previously registered. Method accepts the handle returned by the RegisterEvent method.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; ReRegisterEvent (PVOID EventHandle, int timeout)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">ReRegister interest in the event previously registered with a different timeout. Method accepts the handle returned by the RegisterEvent method.</p>         </td>       </tr>     </tbody></table> </div>  <p class="MsoNormal">&#160;</p>  <h2 class="WordSection1">Class windowsthreadpool::PrivateThreadPool </h2>  <div class="WordSection1">   <table style="border-bottom-style: none; border-right-style: none; border-collapse: collapse; border-top-style: none; border-left-style: none" class="MsoTableLightListAccent5" border="1" cellspacing="0" cellpadding="0"><tbody>       <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; background: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p style="text-align: center" class="MsoNoSpacing" align="center"><b><span style="color: white">Public Member Functions</span></b></p>         </td>          <td style="border-bottom-color: #4bacc6; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; background: #4bacc6; border-bottom-width: 1pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p style="text-align: center" class="MsoNoSpacing" align="center"><b><span style="color: white">Description</span></b></p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>bool&#160; SetThreadpoolMax (size_t max)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Set the maximum number of the threads that can be created in the threadpool. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>bool&#160; SetThreadpoolMin (size_t min)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Set the minimum number of the threads to exist in the threadpool.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItem (Function cb)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to run Function cb. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItem (Function cb, PVOID State)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to run Function cb which accepts a PVOID as parameter. </p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithLowPri (Function cb)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a low priority work item to the process-wide threadpool to run Function cb.</p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithHiPri (Function cb)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a high priority work item to the process-wide threadpool to run Function cb.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithLowPri (Function cb, PVOID State)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a low priority work item to the process-wide threadpool to run Function cb which accepts a PVOID as parameter.</p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; QueueUserWorkItemWithHiPri (Function cb, PVOID State)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a high priority work item to the process-wide threadpool to run Function cb which accepts a PVOID as parameter.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>void&#160; WaitForAll () </b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Wait for all queued work items to finish execution. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>void&#160; WaitForAllCurrentlyRunning ()</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Wait for all running work items to finish execution and cancel any queued work items which haven’t started running. </p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />PVOID&#160; RegisterEvent (Function cb, HANDLE Event, int timeout)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to call Function cb when the event is signaled or the timeout expires. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />PVOID&#160; RegisterEvent (Function cb, PVOID State, HANDLE Event, int timeout)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Queue a work item to the process-wide threadpool to call Function cb which accepts a PVOID as parameter when the event is signaled or the timeout expires.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; DestroyEvent (PVOID EventHandle)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">Cancel any interest in the event previously registered. Method accepts the handle returned by the RegisterEvent method. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; ReRegisterEvent (PVOID EventHandle)</b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">ReRegister interest in the event previously registered. Method accepts the handle returned by the RegisterEvent method.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing"><b>template&lt;class Function &gt;&#160; <br />bool&#160; ReRegisterEvent (PVOID EventHandle, int timeout)</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 239.4pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="319">           <p class="MsoNoSpacing">ReRegister interest in the event previously registered with a different timeout. Method accepts the handle returned by the RegisterEvent method.</p>         </td>       </tr>     </tbody></table> </div>  <p class="MsoNormal">&#160;</p>  <h2 class="WordSection1">Class windowsthreadpool::Timer </h2>  <div class="WordSection1">   <table style="border-bottom-style: none; border-right-style: none; border-collapse: collapse; border-top-style: none; border-left-style: none" class="MsoTableLightListAccent5" border="1" cellspacing="0" cellpadding="0"><tbody>       <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 255.95pt; padding-right: 5.4pt; background: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="341">           <p style="text-align: center; line-height: normal; margin-bottom: 0pt" class="MsoNormal" align="center"><b><span style="color: white">Public Member Functions</span></b></p>         </td>          <td style="border-bottom-color: #4bacc6; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 222.85pt; padding-right: 5.4pt; background: #4bacc6; border-bottom-width: 1pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="297">           <p style="text-align: center; line-height: normal; margin-bottom: 0pt" class="MsoNormal" align="center"><b><span style="color: white">Description</span></b></p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 255.95pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="341">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal"><b>&#160; Timer (size_t mSeconds, Function cb) </b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 222.85pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="297">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal">Constructor which accepts a function cb to call when the time expires in milliseconds. </p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 255.95pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="341">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal"><b>&#160; Timer (size_t mSeconds, size_t Period, Function cb) </b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 222.85pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="297">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal">Constructor which accepts a function cb to call when the time expires in milliseconds. It keeps calls this function at the specified period in milliseconds. </p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 255.95pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="341">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal"><b>&#160; Timer (size_t mSeconds, Function cb, PVOID State) </b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 222.85pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="297">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal">Constructor which accepts a function cb to call and a PVOID parameter which it takes as input when the time expires in milliseconds.</p>         </td>       </tr>        <tr>         <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-style: none; padding-left: 5.4pt; width: 255.95pt; padding-right: 5.4pt; border-top-style: none; padding-top: 0in" valign="top" width="341">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal"><b>&#160; Timer (size_t mSeconds, size_t Period, Function cb, PVOID State) </b></p>         </td>          <td style="border-bottom-style: none; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 222.85pt; padding-right: 5.4pt; border-top-style: none; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="297">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal">Constructor which accepts a function cb to call and a PVOID parameter which it takes as input when the time expires in milliseconds. It keeps calls this function at the specified period in milliseconds.</p>         </td>       </tr>        <tr>         <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; border-right-width: 1pt; padding-left: 5.4pt; width: 255.95pt; padding-right: 5.4pt; border-right-color: #4bacc6; border-top: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="341">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal"><b>void&#160; DestroyTimer ()</b></p>         </td>          <td style="border-bottom: #4bacc6 1pt solid; border-left: #4bacc6 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 222.85pt; padding-right: 5.4pt; border-top: #4bacc6 1pt solid; border-right: #4bacc6 1pt solid; padding-top: 0in" valign="top" width="297">           <p style="line-height: normal; margin-bottom: 0pt" class="MsoNormal">Cancels all interest in the timer. </p>         </td>       </tr>     </tbody></table> </div>]]></content:encoded>
    </item>
    <item>
      <title>Waiting on Events from within Threadpool</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/waiting-on-events-from-within-threadpool</link>
      <pubDate>Thu, 14 Oct 2010 16:20:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/10/14/waiting-on-events-from-within-threadpool/</guid>
      <description><![CDATA[Download source code from MSDN Code Gallery. &nbsp;
Till now we have seen how to use the default...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal" style="margin: 0in 0in 10pt; background: #ddd9c3; mso-background-themecolor: background2; mso-background-themeshade: 230;"><a href="http://code.msdn.microsoft.com/win32threadpool/"><span style="font-family: Calibri; font-size: small;">Download</span></a><span style="font-size: small;"><span style="font-family: Calibri;"> source code from MSDN Code Gallery. <span style="mso-spacerun: yes;">&nbsp;</span></span></span><o:p></o:p></p>
<p class="MsoNormal">Till now we have seen how to use the default process-wide threadpool to submit work items, how to create your own private threadpool and how to create timer objects to execute callbacks when the timer fires. In this post (final one in the threadpool series), we will see how to create waiter objects so that you can run your callback code when an event fires. </p>
<p>&nbsp;</p>
<p class="MsoNormal">Both the windowsthreadpool::SimpleThreadPool and windowsthreadpool::PrivateThreadPool class support the following member functions to support events; RegisterEvent, ReRegisterEvent and DestroyEvent. To wait on an event, you need to call RegisterEvent; once that event fires, you need to call ReRegisterEvent to wait on it again. DestroyEvent closes the wait object in the threadpool so that you no longer wait on that event. </p>
<p class="MsoNormal"><b>Example Code: </b></p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 1pt; padding-right: 1pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing"><span style="font-family: consolas; color: blue; font-size: 9.5pt">void</span><span style="font-family: consolas; font-size: 9.5pt"> CALLBACK HelloWorld2(PVOID state)</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNREFERENCED_PARAMETER(state); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; <span style="color: #a31515">"Hello World2."</span> &lt;&lt; endl; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; color: blue; font-size: 9.5pt">void</span><span style="font-family: consolas; font-size: 9.5pt"> CALLBACK HelloWorld(PVOID state)</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNREFERENCED_PARAMETER(state); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; <span style="color: #a31515">"Hello World."</span> &lt;&lt; endl; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; color: blue; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; color: blue; font-size: 9.5pt">int</span><span style="font-family: consolas; font-size: 9.5pt"> <span style="color: blue">_cdecl</span> _tmain()</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">using</span> <span style="color: blue">namespace</span> windowsthreadpool; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SimpleThreadPool stp; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PrivateThreadPool ptp; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (NULL == hEvent) </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: #a31515">"Could not create event."</span>;&nbsp; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PVOID handle = stp.RegisterEvent(HelloWorld, hEvent, 100000); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetEvent(hEvent);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stp.DestroyEvent&lt;THREADPOOLCALLBACK&gt;(handle); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stp.WaitForAll(); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PVOID phandle = ptp.RegisterEvent(HelloWorld2, hEvent, 100000); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetEvent(hEvent);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(100);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptp.ReRegisterEvent&lt;THREADPOOLCALLBACK&gt;(phandle); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetEvent(hEvent);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(100);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptp.DestroyEvent&lt;THREADPOOLCALLBACK&gt;(phandle); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptp.WaitForAll(); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
</div>
<p class="MsoNormal">In this example, we create an event and register interest in the event by queuing a work item to the process-wide threadpool along with a pointer to our callback (HelloWorld). When the event is set, the callback &ldquo;HelloWorld&rdquo; executes. A different callback (HelloWorld2) is registered with the private threadpool for the same event. This callback will run twice. </p>
<p>&nbsp;</p>
<p class="MsoNormal">The RegisterEvent method takes in a function pointer to the callback function, an event handle and a timeout value. If the timeout is -1, then it waits indefinitely for the event. The RegisterEvent method returns an opaque handle which is passed as input to the DestroyEvent and ReRegisterEvent methods. Once a raised event is handled in the callback, you need to re-register interest in the event for the threadpool to call your callback again.</p>
<p>&nbsp;</p>
<p class="MsoNormal">The majority of the work for these methods is happening in the internal class WaitCallBack. When you register an event, it first creates TP_WAIT object by calling CreateThreadpoolWait using the same environment block for all work items queued via the same threadpool. It then passes this wait object along with the event handle and a timeout value to SetThreadpoolWait. If a user wants to wait indefinitely for the event to fire, then you need to pass NULL as the timeout value to SetThreadpoolWait. </p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">WaitCallBack(<span style="color: blue">const</span> Function Func, PVOID st, PTP_CALLBACK_ENVIRON pEnv, HANDLE Event, <span style="color: blue">int</span> timeout) : m_Func(Func), state(st), m_Event(Event), TimeOut(timeout)&nbsp; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait = CreateThreadpoolWait(callback, <span style="color: blue">this</span>, pEnv); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!wait)</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: #a31515">"Error: Could not create threadpool wait object."</span>;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (timeout &gt; -1)</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILETIME dueTime;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *<span style="color: blue">reinterpret_cast</span>&lt;PLONGLONG&gt;(&amp;dueTime) = -<span style="color: blue">static_cast</span>&lt;LONGLONG&gt;(MILLI_SECOND_TO_NANO100(timeout));</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetThreadpoolWait(wait, Event, &amp;dueTime);</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">else</span> </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetThreadpoolWait(wait, Event, NULL);&nbsp;&nbsp;&nbsp; <span style="color: green">// timeout is -1 which indicates infinite timeout </span></span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
</div>
<p class="MsoNormal">The ReRegisterEvent method simply calls the SetThreadpoolWait as shown above. </p>
<p class="MsoNormal">The DestroyEvent method indicates that you are no longer interested in this event. Remember back in the first post I mentioned that if you call CloseThreadpoolCleanupGroupMembers, it allows you to free all work objects, wait objects and timers associated with the cleanup group in one call. So that&rsquo;s why in the DestroyEvent method, we check if WaitForAll has already been called (which would imply the wait object is already freed) and if not then call the internal DestroyEvent method. </p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">template &lt;class Function&gt;</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">bool DestroyEvent(PVOID EventHandle)</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; windowsthreadpool::internal::WaitCallBack&lt;Function&gt; *wcb = reinterpret_cast&lt;windowsthreadpool::internal::WaitCallBack&lt;Function&gt;*&gt; (EventHandle); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (InfraInitialized)</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wcb-&gt;DestroyEvent(); </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete wcb; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true; </span></p>
<p class="MsoNoSpacing"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
</div>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">And that&rsquo;s it! Over the course of this series on threadpool we have learnt how to queue work items to the default process-wide threadpool, create your own private threadpool, queue work items to it, create a background threadpool which runs low priority work items, create timer objects and wait on events to run code when the events are signaled. </p>
<p>&nbsp;</p>
<p class="MsoNormal">The only functionality that&rsquo;s not covered in this series is using the threadpool to perform asynchronous IO.&nbsp; Maybe I will cover that in a later series. In the meantime, if you have any questions on the win32 threadpool, please leave a comment and I will try to get it answered. </p>
<p class="MsoNormal">&nbsp;</p>
<p>
<p>&nbsp;</p>
</p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Threadpool Timer Functions</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/threadpool-timer-functions</link>
      <pubDate>Wed, 13 Oct 2010 17:30:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/10/13/threadpool-timer-functions/</guid>
      <description><![CDATA[Download source code from MSDN Code Gallery. 
The threadpool provides timer functionality with...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal" style="background:#DDD9C3"><a href="http://code.msdn.microsoft.com/win32threadpool/">Download</a> source code from MSDN Code Gallery. </p>
<p class="MsoNormal">The threadpool provides timer functionality with the SetThreadpoolTimer and CreateThreadpoolTimer functions; the ability to call your functions at specified times.&nbsp; In this blog post I will show how to use the wrapper windowsthreadpool::Timer class provided to obtain timer functionality by using these APIs. &nbsp;</p>
<p class="MsoNormal"><b>Example code: </b></p>
<div style="border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt;
background:#D9D9D9">
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">void CALLBACK Hello(PVOID state)</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">{</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNREFERENCED_PARAMETER(state); </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Hello World - this should occur roughly every second." &lt;&lt; endl; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">}</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">int _cdecl _tmain()</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">{</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using namespace windowsthreadpool; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="text-indent:.5in;background:#D9D9D9;border:none;
padding:0in"><span style="font-size:9.5pt;font-family:Consolas">Timer&lt;THREADPOOLCALLBACK&gt; *t = <span style="color:blue">new</span> Timer&lt;THREADPOOLCALLBACK&gt;(1000, 1000, Hello);&nbsp; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="text-indent:.5in;background:#D9D9D9;border:none;
padding:0in"><span style="font-size:9.5pt;font-family:Consolas">Sleep(10000);</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="text-indent:.5in;background:#D9D9D9;border:none;
padding:0in"><span style="font-size:9.5pt;font-family:Consolas;color:blue">delete</span><span style="font-family: Consolas; font-size: 9.5pt;"> t;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">}</span></p>
</div>
<p class="MsoNormal">The function &ldquo;Hello&rdquo; will be called a 1000 milliseconds from now and from then on will repeatedly be called every 1000 milliseconds until the timer is destroyed by calling &ldquo;delete t&rdquo;. Since there is a Sleep(10000) before the delete call, the function &ldquo;Hello&rdquo; will be called ten times. </p>
<p>&nbsp;</p>
<p class="MsoNormal">The timer class has four constructors which let you create timer objects and when the object goes out of scope the timer is destroyed or you can explicitly call DestroyTimer method to cancel the timer. The timer class accepts THREADPOOLCALLBACK as the template type; this type is defined as a function pointer accepting a PVOID as a parameter and returning nothing.&nbsp; The timer class needs this definition because it needs to know the function signature of the callback method (Hello). </p>
<div style="border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt;
background:#D9D9D9">
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: #00b050; font-size: 9.5pt;">// Typedef for a threadpool function accepting one parameter and </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: #00b050; font-size: 9.5pt;">// returning void</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: #00b050; font-size: 9.5pt;">//</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">typedef</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color:blue">void</span> (CALLBACK* THREADPOOLCALLBACK)(PVOID);</span></p>
</div>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The timer constructor accepts the starting time in milliseconds for the timer to expire, a period in milliseconds if the timer is expected to fire periodically, a function pointer to the callback and a pointer to the parameter if required. If the period is zero, the timer is a one-time timer and will fire only once. </p>
<p class="MsoNormal">To close the timer you can explicitly call DestroyTimer() or if the timer goes out of scope, then its destructor will close the timer object. To close a timer in the threadpool, you have to call SetThreadpoolTimer with the pftDueTime parameter set to NULL and the msPeriod and msWindowLength parameters set to 0. The timer object is then closed with CloseThreadpoolTimer. </p>
<div style="border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt;
background:#D9D9D9">
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> DestroyTimer()</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">{</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">delete</span> tcb; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tcb = NULL; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">}</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; color: green; font-size: 9.5pt;">// Destructor: cancels the timer when the object is destructed.</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">~Timer()</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">{</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if</span> (tcb)</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DestroyTimer();</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">}</span></p>
</div>
<p class="MsoNormal">The timer class uses the TimerCallback class internally to create timer objects in the default process-wide threadpool, set the appropriate time and period and destroy the timer object when it goes out of scope. </p>
<p>&nbsp;</p>
<div style="border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt;
background:#D9D9D9">
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">template &lt;class Function&gt;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">class TimerCallBack</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">{</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">private: </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const Function m_Func;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PVOID state;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PTP_TIMER timer;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static void CALLBACK callback (PTP_CALLBACK_INSTANCE Instance, PVOID Param, PTP_TIMER timer)</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TimerCallBack&lt;Function&gt; *pc = reinterpret_cast&lt;TimerCallBack&lt;Function&gt;*&gt;(Param);</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc-&gt;m_Func(pc-&gt;state);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Call the user provided function</span></span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">public:</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TimerCallBack(const Function Func, PVOID st, PTP_CALLBACK_ENVIRON pEnv, size_t mSeconds, size_t Period) : m_Func(Func), state(st)</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer = CreateThreadpoolTimer(callback, this, pEnv); </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!timer)</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw <span style="color: #a31515;">"Error: Could not create threadpool timer."</span>;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// A negative due time indicates a time relative to now.</span></span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//</span></span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILETIME dueTime;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *reinterpret_cast&lt;PLONGLONG&gt;(&amp;dueTime) = -static_cast&lt;LONGLONG&gt;(MILLI_SECOND_TO_NANO100(mSeconds));</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetThreadpoolTimer(timer, &amp;dueTime, Period, ACCEPTABLELAG); </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Destructor - cancels the timer when the object is destructed. </span></span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~TimerCallBack()</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Prevent callbacks after timer is closed</span></span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetThreadpoolTimer(timer, NULL, 0, 0); </span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseThreadpoolTimer(timer);</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="background:#D9D9D9;border:none;padding:0in"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;</span></p>
</div>
<p class="MsoNormal">Notice the ACCEPTABLELAG parameter to the SetThreadpoolTimer call. The last parameter is the amount of time that you can wait for this timer to fire once it expires. ACCEPTABLELAG is defined in timer.h. </p>
<p>&nbsp;</p>
<p class="MsoNormal"><b>Example</b>: If you have two timers set within a process, one fires after 100milliseconds and another fires after 110milliseconds. Windows will now have to set off the first timer and 10mSecs later set off the next timer. However, if the first timer had set the last parameter to 20milliseconds which means it&rsquo;s acceptable for the timer to fire as late as 120milliseconds from now, than Windows would only need to do work once. At time 110milliseconds from now, Windows will fire both the timers and both callbacks will run. This is generally known as timer coalescing and is useful for Windows to prevent unnecessary timers from firing and decreasing battery life. More information about timer coalescing available <a href="http://www.microsoft.com/whdc/system/pnppwr/powermgmt/TimerCoal.mspx">here</a>. </p>
<p class="MsoNormal">Next up, how to call functions when events are fired&hellip;</p>
<p class="MsoNormal">&nbsp;</p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Introduction to the Windows Threadpool (part 2)</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/introduction-to-the-windows-threadpool-part-2</link>
      <pubDate>Tue, 12 Oct 2010 10:30:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/10/12/introduction-to-the-windows-threadpool-part-2/</guid>
      <description><![CDATA[Download source code from MSDN Code Gallery. 
In the previous posts, I talked about using the...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal" style="background: #ddd9c3"><a href="http://code.msdn.microsoft.com/win32threadpool/">Download</a> source code from MSDN Code Gallery. </p>
<p class="MsoNormal">In the previous posts, I talked about using the windowsthreadpool::SimpleThreadPool class to queue work items to the process-wide threadpool and queuing work items with different priorities.&nbsp; In this post, I will show how to create your own private threadpool using the class windowsthreadpool::PrivateThreadPool. </p>
<h2>Private Threadpool </h2>
<p class="MsoNormal">The private threadpool allows a developer to have their own pool and control work items that are queued to the threadpool. In addition, having a private threadpool also allows the developer to control the maximum and minimum of threads the threadpool can create. </p>
<p>&nbsp;</p>
<p class="MsoNormal">To create a private threadpool, you need to call CreateThreadpool which returns a handle to the pool object, which is done in the constructor of the class PrivateThreadPool. The private pool is closed in the destructor by calling CloseThreadpool. </p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">PrivateThreadPool(): InfraInitialized(<span style="color: blue">false</span>)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pool = CreateThreadpool(NULL); </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!Pool)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: #a31515">"Error: CreateThreadpool Failed."</span>;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">} </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">~PrivateThreadPool()</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseThreadpool(Pool); </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
</div>
<p class="MsoNormal">You queue work items to the private threadpool using the same methods (QueueUserWorkItem) as the simple threadpool class and wait for the work items to finish using the same WaitForAll. Because we are using a private threadpool, we need to associate our callback environment block with our private threadpool, so that all work items submitted using this environment block are submitted to the private threadpool. This is done in the InitializeInfra method by calling the function SetThreadpoolCallbackPool. </p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">void</span><span style="font-family: consolas; font-size: 9.5pt"> InitializeInfra()</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{<span style="color: green"> </span></span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!InfraInitialized)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InitializeThreadpoolEnvironment(&amp;CallbackEnvironment);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetThreadpoolCallbackPool(&amp;CallbackEnvironment, Pool);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CleanupGroup = CreateThreadpoolCleanupGroup();</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetThreadpoolCallbackCleanupGroup(&amp;CallbackEnvironment, CleanupGroup, NULL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InfraInitialized = true; </span></p>
</div>
<p class="MsoNormal">Submitting work items to a private threadpool requires using function SubmitThreadpoolWork. This function takes in a pointer to a TP_WORK object which is created with the CreateThreadpoolWork function. The work is submitted using an internal helper class WorkCallBack which is similar to the helper class SimpleThreadPool uses except in this it uses SubmitThreadpoolwork to submit work items. The WorkCallBack instance is destroyed in the callback method after the user callback runs.&nbsp; </p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">// Internal class which submits the work items </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">template</span><span style="font-family: consolas; font-size: 9.5pt"> &lt;<span style="color: blue">class</span> Function&gt;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">class</span><span style="font-family: consolas; font-size: 9.5pt"> WorkCallBack</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">private</span><span style="font-family: consolas; font-size: 9.5pt">: </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">const</span> Function m_Func;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PVOID state;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PTP_WORK work;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">static</span> <span style="color: blue">void</span> CALLBACK callback (PTP_CALLBACK_INSTANCE Instance, PVOID Param, PTP_WORK work)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNREFERENCED_PARAMETER(Instance);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNREFERENCED_PARAMETER(work);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WorkCallBack&lt;Function&gt; *pc = <span style="color: blue">reinterpret_cast</span>&lt;WorkCallBack&lt;Function&gt;*&gt;(Param);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc-&gt;m_Func(pc-&gt;state);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">delete</span> pc; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span>; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">public</span><span style="font-family: consolas; font-size: 9.5pt">:</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WorkCallBack(<span style="color: blue">const</span> Function Func, PVOID st, PTP_CALLBACK_ENVIRON pEnv) : m_Func(Func), state(st)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; work = CreateThreadpoolWork(callback, <span style="color: blue">this</span>, pEnv); </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!work)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: #a31515">"Error: Could not create threadpool work."</span>;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SubmitThreadpoolWork(work);</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
</div>
<p class="MsoNormal">The private threadpool supports two operations that are not supported by the default process wide threadpool, setting the maximum and minimum number of threads that the threadpool can create. It should be pretty obvious why these operations are not supported on the default process-wide threadpool; the process-wide threadpool is also used by the system to execute its work items, so you don&rsquo;t want the developer to set arbitrary limits on this threadpool and accidentally impact system performance. If you are creating your own private threadpool, you shouldn&rsquo;t have to set the maximum and minimum number of threads; the system is capable of managing thread lifetimes on its own and unless you really notice some problem don&rsquo;t have to change these settings. </p>
<p>&nbsp;</p>
<p class="MsoNormal">As mentioned in the previous post, if you have low priority work items, instead of modifying the thread&rsquo;s priority in the callback, you should use a private threadpool. In the example below I am creating a background threadpool which can have a maximum of only one thread. So at any given time only one work item can execute. And if there are no more work items to execute the threadpool will kill that thread as well as determined by its internal heuristics. </p>
<p class="MsoNormal"><b>Example Code:</b></p>
<div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt">
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">void</span><span style="font-family: consolas; font-size: 9.5pt"> CALLBACK PrintI(PVOID state)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> *i = <span style="color: blue">reinterpret_cast</span>&lt;<span style="color: blue">int</span> *&gt; (state); </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; *i &lt;&lt; <span style="color: #a31515">" ,"</span> &lt;&lt; endl; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: green; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">int _cdecl _tmain()</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using namespace windowsthreadpool; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: green; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: green; font-size: 9.5pt">// Background threadpool - only one thread running</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: green; font-size: 9.5pt">// work items are executed in FIFO order </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: green; font-size: 9.5pt">//</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">PrivateThreadPool backgroundworker;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">backgroundworker.SetThreadpoolMax(1); </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; color: blue; font-size: 9.5pt">for</span><span style="font-family: consolas; font-size: 9.5pt">(<span style="color: blue">int</span> i=0; i&lt;10; ++i)</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">{</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; backgroundworker.QueueUserWorkItemWithLowPri(PrintI, &amp;arr[i]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">}</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">&nbsp;</span></p>
<p class="MsoNoSpacing" style="border-bottom-style: none; padding-bottom: 0in; border-right-style: none; padding-left: 0in; padding-right: 0in; border-top-style: none; background: #d9d9d9; border-left-style: none; padding-top: 0in"><span style="font-family: consolas; font-size: 9.5pt">backgroundworker.WaitForAll(); </span></p>
</div>
<p class="MsoNormal">And that&rsquo;s it, your own private threadpool. As the private threadpool only has one thread, each work item executes serially, so technically we don&rsquo;t need to queue with low priority, the same behavior can be obtained by just queuing normally. </p>
<p>&nbsp;</p>
<p class="MsoNormal">Since this is a private threadpool to run background work items, if you really want you could even modify the thread priority in the work item. However, note that if you do modify the thread priority in the work item, the priority needs to be reset back to the original value before the work item returns. </p>
<p>&nbsp;</p>
<p class="MsoNormal">Next up; creating a timer class using the win32 threadpool functions... </p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Threadpool Work item priority</title>
      <link>https://docs.microsoft.com/archive/blogs/harip/threadpool-work-item-priority</link>
      <pubDate>Mon, 11 Oct 2010 10:29:00 GMT</pubDate>
      <dc:creator><![CDATA[Hari Pulapaka - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harip/2010/10/11/threadpool-work-item-priority/</guid>
      <description><![CDATA[Download source code from MSDN Code Gallery.       In the previous post, I talked about how to queue...]]></description>
      <content:encoded><![CDATA[<p style="background: #ddd9c3; mso-background-themecolor: background2; mso-background-themeshade: 230" class="MsoNormal"><a href="http://code.msdn.microsoft.com/win32threadpool/">Download</a> source code from MSDN Code Gallery. </p>  <p></p>  <p></p>  <p class="MsoNormal">In the previous post, I talked about how to queue work items to the process-wide system threadpool using TrySubmitThreadpoolCallback. In this post, I will talk about work item priority and how to queue low priority work items.<span style="mso-spacerun: yes">&#160; </span></p>  <p></p>  <p></p>  <p class="MsoNormal">The thread that your work item executes is not owned by your application; it’s owned and managed by the system. That implies the application should not modify the state because it has no visibility into other work items that might be scheduled to run on this thread. Depending on how the state was modified, other work items may not react well. </p>  <p></p>  <p></p>  <p class="MsoNormal">One of the most common ways developers modify thread state is by lowering the thread priority in the work item. This is a no-no and has unintended consequences, even if you reset the thread’s priority before returning from your work item. </p>  <p></p>  <p></p>  <p class="MsoNormal">Here’s why: In Windows 7, the threadpool uses something called a concurrency control to control the number of concurrently running threads. This number is typically equal to the number of cores on the machine; if a machine has two cores, then the threadpool will have a concurrency control number of 2. This seems quite logical, if you have two cores, only two threads can execute code. <b style="mso-bidi-font-weight: normal">Note:</b> If a work item blocks for any reason, then a new thread is created by the threadpool to execute other work items so all cores stay busy. </p>  <p></p>  <p></p>  <p class="MsoNormal">However, if the work item lowers the priority of the thread below normal and there is another normal priority thread in the system which is ready to run, then the Windows scheduler schedules the normal priority thread and preempts the lower priority threadpool work item thread. This is bad because the threadpool sees that the thread whose priority has been lowered is still in a ready state. A thread in ready state counts towards your concurrently running threads, because technically it could run as soon as it is scheduled. Therefore, the threadpool thinks it has two running threads in the system. However, only one thread is actually running, so work items in the threadpool will get starved. This is especially egregious when it happens in the shared process-wide threadpool which has system work items, because it could impact system-wide performance. </p>  <p></p>  <p></p>  <p><b>Note</b>: if you increase the thread priority in the callback, then it may not starve the other work items the same way as theoretically the callback should now finish quicker. However, this is still not recommended especially if you are in the process-wide system threadpool as the thread is not yours to modify. Plus if everyone decides to boost their priority this way then work items will be starved and the system performance will be impacted. </p>  <p></p>  <p></p>  <p class="MsoNormal">This is not a theoretical concern and is quite real based on our experience. If you have any work items that need to execute at lower priority, there are two alternatives:</p>  <ol>   <li>     <div class="MsoNormal">Create your own private threadpool which services only your work items and set their priorities as needed. <span style="mso-spacerun: yes">&#160;</span>If these work items are starved or executed slowly the Windows system is not affected. Later in the series I will show how to use the wrapper class windowsthreadpool::PrivateThreadPool to do exactly this. </div>   </li>    <li>     <div class="MsoNormal">Use the threadpool function SetThreadpoolCallbackPriority to set the priority on a work item. This function does not modify thread priorities; instead it uses separate queues for low, high and normal priority work items. The MSDN documentation hints at this:        <p></p>     </div>   </li> </ol>  <div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d6e3bc; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt; mso-background-themecolor: accent3; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt; mso-background-themetint: 102">   <h3 style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; background: #d6e3bc; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: accent3; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-background-themetint: 102"><span style="line-height: 115%; font-family: consolas; color: windowtext; font-size: 9pt"><a href="http://msdn.microsoft.com/en-us/library/dd405519(VS.85).aspx">http://msdn.microsoft.com/en-us/library/dd405519(VS.85).aspx</a>         <p></p>     </span></h3>    <h3 style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; background: #d6e3bc; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: accent3; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-background-themetint: 102"><span style="color: windowtext">Remarks        <p></p>     </span></h3>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; background: #d6e3bc; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: accent3; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; mso-background-themetint: 102">Higher priority callbacks are guaranteed to be run first by the first available worker thread, but they are not guaranteed to finish before lower priority callbacks. </p>    <p></p>    <p></p> </div>  <p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal">The QueueUserWorkItemWithHiPri and QueueUserWorkItemWithLowPri methods in the SimpleThreadPool class use the SetThreadpoolCallbackPriority function to change work item priority. We create a new environment structure, initialize the structure, set the appropriate priority and associate it with our cleanup group so that we can wait on the work items using the WaitForAll method. </p>  <p></p>  <p></p>  <p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"></p>  <p>&#160;</p>  <p></p>  <div style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; background: #d9d9d9; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 1pt; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt">   <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; color: blue; font-size: 9.5pt">template</span><span style="font-family: consolas; font-size: 9.5pt"> &lt;<span style="color: blue">class</span> Function&gt; </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; color: blue; font-size: 9.5pt">bool</span><span style="font-family: consolas; font-size: 9.5pt"> QueueUserWorkItemWithLowPri(Function cb) </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt">{ </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>InitializeInfra(); <span style="mso-tab-count: 1">&#160; </span></span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>TP_CALLBACK_ENVIRON LowPriCallbackEnvironment; </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>InitializeThreadpoolEnvironment(&amp;LowPriCallbackEnvironment); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>SetThreadpoolCallbackPriority(&amp;LowPriCallbackEnvironment, TP_CALLBACK_PRIORITY_LOW); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>SetThreadpoolCallbackCleanupGroup(&amp;LowPriCallbackEnvironment, CleanupGroup, NULL); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"></span>      <p>&#160;</p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">return</span> QueueUserWorkItemInternal(cb, NULL, &amp;LowPriCallbackEnvironment); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt">} </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"></span>      <p>&#160;</p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; color: blue; font-size: 9.5pt">template</span><span style="font-family: consolas; font-size: 9.5pt"> &lt;<span style="color: blue">class</span> Function&gt; </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; color: blue; font-size: 9.5pt">bool</span><span style="font-family: consolas; font-size: 9.5pt"> QueueUserWorkItemWithHiPri(Function cb) </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt">{ </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>InitializeInfra(); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>TP_CALLBACK_ENVIRON HiPriCallbackEnvironment; </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>InitializeThreadpoolEnvironment(&amp;HiPriCallbackEnvironment); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>SetThreadpoolCallbackPriority(&amp;HiPriCallbackEnvironment, TP_CALLBACK_PRIORITY_HIGH); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span>SetThreadpoolCallbackCleanupGroup(&amp;HiPriCallbackEnvironment, CleanupGroup, NULL); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"></span>      <p>&#160;</p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">return</span> QueueUserWorkItemInternal(cb, NULL, &amp;HiPriCallbackEnvironment); </span>      <p></p>   </p>    <p></p>    <p style="border-bottom: medium none; border-left: medium none; padding-bottom: 0in; line-height: normal; padding-left: 0in; padding-right: 0in; margin-bottom: 0pt; background: #d9d9d9; border-top: medium none; border-right: medium none; padding-top: 0in; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt" class="MsoNormal"><span style="font-family: consolas; font-size: 9.5pt">} </span>      <p></p>   </p>    <p></p> </div>  <p class="MsoNormal"></p>  <p>&#160;</p>  <p></p>  <p class="MsoNormal">Next up, how to create your own private threadpool… </p>]]></content:encoded>
    </item>
  </channel>
</rss>