Click here to Skip to main content
15,883,883 members
Articles / General Programming / Threads

Smart Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.96/5 (314 votes)
27 Aug 2012Ms-PL40 min read 2.2M   29.1K   1.1K  
A .NET Thread Pool fully implemented in C# with many features.
using System;
using System.Threading;

using NUnit.Framework;

using Amib.Threading;

namespace WorkItemsGroupTests
{
	/// <summary>
	/// Summary description for TestWIGConcurrency.
	/// </summary>
	[TestFixture]
	[Category("TestWIGConcurrency")]
	public class TestWIGConcurrency
	{
		private Random _randGen;
		private int [] _concurrentOps;
		private int _concurrencyPerWig;
		private bool _success;

	    [Test]
		public void TestConcurrencies()
		{
			Concurrency(1, 1, 10);
			Concurrency(1, 1, 100);

			Concurrency(1, 5, 10);
			Concurrency(1, 5, 100);

			Concurrency(5, 5, 10);
			Concurrency(5, 5, 100);
		}

		private void Concurrency(
			int concurrencyPerWig,
			int wigsCount,
			int workItemsCount)
		{
			Console.WriteLine(
				"Testing : concurrencyPerWig = {0}, wigsCount = {1}, workItemsCount = {2}",
				concurrencyPerWig,
				wigsCount,
				workItemsCount);

			_success = true;
			_concurrencyPerWig = concurrencyPerWig;
			_randGen = new Random(0);

			STPStartInfo stpStartInfo = new STPStartInfo();
			stpStartInfo.StartSuspended = true;

			SmartThreadPool stp = new SmartThreadPool(stpStartInfo);

			_concurrentOps = new int[wigsCount];

			IWorkItemsGroup [] wigs = new IWorkItemsGroup[wigsCount];

			for(int i = 0; i < wigs.Length; ++i)
			{
				wigs[i] = stp.CreateWorkItemsGroup(_concurrencyPerWig);
				for(int j = 0; j < workItemsCount; ++j)
				{
					wigs[i].QueueWorkItem(new WorkItemCallback(this.DoWork), i);
				}

				wigs[i].Start();
			}

			stp.Start();

			stp.WaitForIdle();

			Assert.IsTrue(_success);

			stp.Shutdown();
		}

		private object DoWork(object state)
		{
			int wigsIndex = (int)state;

			int val = Interlocked.Increment(ref _concurrentOps[wigsIndex]);
			_success = _success && (val <= _concurrencyPerWig);

			int waitTime = _randGen.Next(50);
			Thread.Sleep(waitTime);

			val = Interlocked.Decrement(ref _concurrentOps[wigsIndex]);
			_success = _success && (val >= 0);

			return null;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Israel Israel
B.Sc. in Computer Science.
Works as Software Developer.

Comments and Discussions