C# Source Code: Queuing Work Items to the Thread Pool
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Queuing Work Items to the Thread Pool
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Saturday, April 10, 2004
Hits:
626
Category:
Threading/Asynchronous operations
Article:
Systems are generally written as multithreaded when they require the highest levels of performance (eg trading systems). However thread isn't free and combining this with the cost of context switching will add to the overall cost of executing your application. By implimenting a thread pool much of the overhead associated with creating threads can be avoided. The code below gives a simple demonstration of how to add items to a the thread pool provided by the runtime: using System; using System.Threading; //Copyright Andrew Baker (www.vbusers.com) namespace vbUsers.Threading { class clsDemoThreadQ { //This class demonstates how to use a Thread Pool [STAThread] static void Main(string[] args) { WorkItem Wrk1 = new WorkItem(); //Add the item to the thread pool int iCompletionPortThreads, iWorkerThreads; ThreadPool.GetAvailableThreads(out iWorkerThreads,out iCompletionPortThreads); Console.WriteLine("Main Thread {0}. There are {1} threads available in the thread pool",AppDomain.GetCurrentThreadId(),iWorkerThreads); //Add the job to the ThreadPool Wrk1.NumJobs = 5; for(int i=0; i
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet