Updated SmartThreadPool article

This commit is contained in:
Ami Bar
2012-08-21 09:00:32 +03:00
parent 9b69e03cf5
commit e1f484c914
2 changed files with 51 additions and 7 deletions
+51 -7
View File
@@ -48,6 +48,14 @@
<t>Performance Counters (Windows &amp; internal)</t><t></t></li> <t>Performance Counters (Windows &amp; internal)</t><t></t></li>
<li> <li>
Work Item timeout (passive)</li> Work Item timeout (passive)</li>
<li>
Threads ApartmentState</li>
<li>
Threads IsBakcground</li>
<li>
Threads name template</li>
<li>
Windows Phone is supported (limited)</li>
</UL> </UL>
<H2>Why do you need a thread pool?</H2> <H2>Why do you need a thread pool?</H2>
<BLOCKQUOTE> <BLOCKQUOTE>
@@ -109,6 +117,15 @@
<ol> <ol>
<li value="27"><A href="#Feature27">Added work item timeout (passive).</A></li> <li value="27"><A href="#Feature27">Added work item timeout (passive).</A></li>
</ol> </ol>
<h2>
New features added in August 2012</h2>
<ol>
<li value="28"><A href="#Feature28">Enabled to set threads IsBackground.</A></li>
<li value="29"><A href="#Feature29">Enabled to set threads ApartmentState.</A></li>
<li value="30"><A href="#Feature30">Added support fore Windows Phone (limited).</A></li>
</ol>
<p></p>
<H2>What about the .NET ThreadPool?</H2> <H2>What about the .NET ThreadPool?</H2>
<P>The Windows system provides one .NET ThreadPool for each process. The .NET ThreadPool can contain up to 25 (by default) threads per processor. It is also stated that the operations in .NET ThreadPool should be quick to avoid suspension of the work of others who use the .NET ThreadPool. Note that several AppDomains in the same process share the same .NET ThreadPool. If you want a thread to work for a long period of time, then the .NET ThreadPool is not a good choice for you (unless you know what you are doing). Note that each asynchronous method call from the .NET Framework that begins with "Begin…" (e.g., <code>BeginInvoke</code>, <code>BeginSend</code>, <code>BeginReceive</code>, etc.) uses the .NET ThreadPool to run its callback. Also note that the .NET ThreadPool <P>The Windows system provides one .NET ThreadPool for each process. The .NET ThreadPool can contain up to 25 (by default) threads per processor. It is also stated that the operations in .NET ThreadPool should be quick to avoid suspension of the work of others who use the .NET ThreadPool. Note that several AppDomains in the same process share the same .NET ThreadPool. If you want a thread to work for a long period of time, then the .NET ThreadPool is not a good choice for you (unless you know what you are doing). Note that each asynchronous method call from the .NET Framework that begins with "Begin…" (e.g., <code>BeginInvoke</code>, <code>BeginSend</code>, <code>BeginReceive</code>, etc.) uses the .NET ThreadPool to run its callback. Also note that the .NET ThreadPool
doesn't support calls to COM with single threaded apartment (STA), since the ThreadPool doesn't support calls to COM with single threaded apartment (STA), since the ThreadPool
@@ -764,8 +781,24 @@ to simplfy the initiation of a parallel task.</p>
automatically if it didn't complete. The cancel works the same as a call to Cancel with the <code>abortExecution</code> argument set to false (This is why the timeout is passive).</p> automatically if it didn't complete. The cancel works the same as a call to Cancel with the <code>abortExecution</code> argument set to false (This is why the timeout is passive).</p>
<p>To sample the cancel use <code>SmartThreadPool.IsWorkItemCanceled</code> which is a static property, or you can use <code>SmartThreadPool.AbortOnWorkItemCancel</code> <p>To sample the cancel use <code>SmartThreadPool.IsWorkItemCanceled</code> which is a static property, or you can use <code>SmartThreadPool.AbortOnWorkItemCancel</code>
which check if the current work item is cancelled and if so abort the thread (<code>Thread.CurrentThread.Abort()</code>).</p> which check if the current work item is cancelled and if so abort the thread (<code>Thread.CurrentThread.Abort()</code>).</p>
See timeout <A href="#TimeoutExample">example</A> below See timeout <A href="#TimeoutExample">example</A> below</p>
<li><a name=Feature28><b>Enabled to set threads IsBackground</b></a></li>
<p>This featue enables to set the <code>IsBackground</code> of the STP threads. The default is <code>true</code>.</p>
<p>To use it:<br />
<code>STPStartInfo stpStartInfo = new STPStartInfo();<br />
stpStartInfo.AreThreadsBackground = false;</code>
</p> </p>
<li><a name=Feature29><b>Enabled to set threads ApartmentState</b></a></li>
<p>This featue enables to set the <code>ApartmentState</code> of the STP threads. The default is not to set.</p>
<p>To use it:<br />
<code>STPStartInfo stpStartInfo = new STPStartInfo();<br />
stpStartInfo.ApartmentState = ApartmentState.MTA;</code>
</p>
<li>S<a name=Feature30><b>upport Windows Phone (limited)</b></a></li>
<p>Add reference to SmartThreadPoolWP.dll in your code and use it.</p>
<P><IMG src="WindowsPhone.png" width="404" height="749"></P>
</OL> </OL>
<H2>When to use?</H2> <H2>When to use?</H2>
<P>The Smart Thread Pool is good when your work items don't do too much, but wait for events, IOs, sockets, etc. This means that the work items don't use CPU, but run for a long time. It is also good when you don't need to keep alive too many threads in the air all the time. If your work items do a short time work, then use the .NET ThreadPool. If you have a constant heavy load of work, then use Toub's thread pool and define the maximum number of threads accordingly.</P> <P>The Smart Thread Pool is good when your work items don't do too much, but wait for events, IOs, sockets, etc. This means that the work items don't use CPU, but run for a long time. It is also good when you don't need to keep alive too many threads in the air all the time. If your work items do a short time work, then use the .NET ThreadPool. If you have a constant heavy load of work, then use Toub's thread pool and define the maximum number of threads accordingly.</P>
@@ -1405,16 +1438,27 @@ public class <A name=TimeoutExample>TimeoutExample</A>
</ul> </ul>
</li><li>7th April 2009: Changes </li><li>7th April 2009: Changes
<ul> <ul>
<li><strong class="style1">Added support for Silverlight and Mono.</strong></li> <li>Added support for Silverlight and Mono.</li>
<li><strong class="style1">Added Join, Choice, and Pipe to SmartThreadPool.</strong></li> <li>Added Join, Choice, and Pipe to SmartThreadPool.</li>
<li><strong class="style1">Added local performance counters (for Mono, Silverlight, and WindowsCE)</strong></li> <li>Added local performance counters (for Mono, Silverlight, and WindowsCE)</li>
<li><strong class="style1">Changed duration measures from DateTime.Now to Stopwatch.</strong></li> <li>Changed duration measures from DateTime.Now to Stopwatch.</li>
<li><strong class="style1">Queues changed from System.Collections.Queue to System.Collections.Generic.LinkedList&lt;T&gt;.</strong></li> <li>Queues changed from System.Collections.Queue to System.Collections.Generic.LinkedList&lt;T&gt;.</li>
</ul> </ul>
</li> </li>
<li>21st December 2009: Changes <li>21st December 2009: Changes
<ul> <ul>
<li><strong class="style1">Added work item timeout (passive).</strong></li> <li>Added work item timeout (passive).</li>
</ul>
</li>
<li>20 August 2012: Changes
<ul>
<li><strong class="style1">Added set name to threads</strong></li>
<li><strong class="style1">Fixed the WorkItemsQueue.Dequeue.<br />
Replaced <code>while(!Monitor.TryEnter(this));</code> with <code>lock(this) { ... }</code></strong></li>
<li><strong class="style1">Fixed SmartThreadPool.Pipe</strong></li>
<li><strong class="style1">Added IsBackground option to threads</strong></li>
<li><strong class="style1">Added ApartmentState to threads</strong></li>
<li><strong class="style1">Fixed thread creation when queuing many work items at the same time.</strong></li>
</ul> </ul>
</li> </li>
</UL> </UL>
Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB