Click here to Skip to main content
15,886,857 members
Articles / High Performance Computing / Parallel Processing

TLS: An exercise in concurrent programming

Rate me:
Please Sign up or sign in to vote.
4.48/5 (11 votes)
11 May 2008CPOL18 min read 39.8K   330   44  
A walkthough about multi-threading an app and a useful helper class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TLS
{

	//=============================================================================================

	abstract class TestBase
	{

		//-----------------------------------------------------------------------------------------

		public abstract int Factor { get; }
		public abstract bool Multithreaded { get; }
		public virtual bool Block { get { return false; } }

		public virtual long TotalWork { get { return Glob.Repetitions * Factor * ( Multithreaded ? Environment.ProcessorCount : 1 ); } }
		public virtual long Done { get; protected set; }

		//-----------------------------------------------------------------------------------------
		// start and finish

		public void Run()
		{
			if ( Block ) WorkMaster();
			else new Thread( new ThreadStart( WorkMaster ) ) { IsBackground = true }.Start();
		}

		void WorkMaster()
		{
			Done = 0;

			Work();

			RaiseFinished();
		}

		protected abstract void Work();

		public event EventHandler Finished;

		protected void RaiseFinished()
		{
			var d = Finished;
			if ( d != null ) d( null, EventArgs.Empty );
		}

		//-----------------------------------------------------------------------------------------
		// shared work members

		void DoWorkCore( Action<long, State> work, State state )
		{
			var oldPriority = Thread.CurrentThread.Priority;

			Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;

			try
			{
				Glob.Stop = false;
				var max = Glob.Repetitions * Factor;
				for ( long i = 0 ; i < max && !Glob.Stop ; ++i ) work( i, state );
			}
			finally
			{
				Thread.CurrentThread.Priority = oldPriority;
			}
		}

		//-----------------------------------------------------------------------------------------
		// non-TLS work members

		public delegate void WorkDelegate( long i );

		public Action[] CreateActions( WorkDelegate work )
		{
			var actions = new Action[ Environment.ProcessorCount ];

			for ( int i = 0 ; i < actions.Length ; i++ )
				actions[ i ] = new Action( delegate { DoWork( work ); } );

			return actions;
		}

		public void DoWork( WorkDelegate work )
		{
			DoWorkCore( ( i, state ) => work( i ), null );
		}

		//-----------------------------------------------------------------------------------------
		// TLS work members

		protected class State
		{
			public long Done = 0;
		}

		Common.TLS<State> _TLS = null;

		protected Common.TLS<State> TLS { get { return _TLS; } set { _TLS = value; } }

		public delegate void WorkDelegate<DATA>( long i, DATA state );

		protected Action[] CreateActions( WorkDelegate<State> work )
		{
			var actions = new Action[ Environment.ProcessorCount ];

			for ( int i = 0 ; i < actions.Length ; i++ )
				actions[ i ] = new Action( delegate { DoWork( work ); } );

			return actions;
		}

		protected void DoWork( WorkDelegate<State> work )
		{
			DoWorkCore( ( i, state ) => work( i, state ), _TLS.Current );
		}

		//-----------------------------------------------------------------------------------------

	}

	//=============================================================================================

}

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 Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions