Click here to Skip to main content
15,886,873 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 SmartThreadPoolTests
{
	/// <summary>
    /// Summary description for TestParallelMethods.
	/// </summary>
	[TestFixture]
	[Category("TestParallelMethods")]
    public class TestParallelMethods
	{
		[Test]
		public void TestJoin() 
		{ 
			SmartThreadPool stp = new SmartThreadPool();

		    SafeCounter sc = new SafeCounter();

            stp.Join(
                sc.Increment,
                sc.Increment,
                sc.Increment);

            Assert.AreEqual(3, sc.Counter);

		    for (int j = 0; j < 10; j++)
		    {
                sc.Reset();

                Action[] actions = new Action[1000];
                for (int i = 0; i < actions.Length; i++)
                {
                    actions[i] = sc.Increment;
                }

                stp.Join(actions);

                Assert.AreEqual(actions.Length, sc.Counter);
		    }

            stp.Shutdown();
		}

	    private class SafeCounter
        {
            private int _counter;

            public void Increment()
            {
                Interlocked.Increment(ref _counter);
            }

            public int Counter
            {
                get { return _counter; }
            }

            public void Reset()
            {
                _counter = 0;
            }
        }

	    [Test]
	    public void TestChoice() 
	    { 
	        SmartThreadPool stp = new SmartThreadPool();

	        int index = stp.Choice(
	            () => Thread.Sleep(1000),
	            () => Thread.Sleep(1500),
	            () => Thread.Sleep(500));

	        Assert.AreEqual(2, index);  
            
	        index = stp.Choice(
	            () => Thread.Sleep(300),
	            () => Thread.Sleep(100),
	            () => Thread.Sleep(200));

	        Assert.AreEqual(1, index);

	        stp.Shutdown();
	    } 
        
        [Test]
	    public void TestPipe() 
	    { 
            SafeCounter sc = new SafeCounter();
	        SmartThreadPool stp = new SmartThreadPool();

            stp.Pipe(
                sc,
                sc1 => { if (sc.Counter == 0) { sc1.Increment(); }}, 
                sc1 => { if (sc.Counter == 1) { sc1.Increment(); }}, 
                sc1 => { if (sc.Counter == 2) { sc1.Increment(); }} 
                );

            Assert.AreEqual(3, sc.Counter);

	        stp.Shutdown();
	    }
	}
}

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