Click here to Skip to main content
12,302,307 members (62,994 online)
Click here to Skip to main content

Stats

22.2K views
221 downloads
44 bookmarked
Posted

TLS: An exercise in concurrent programming

, 11 May 2008 CPOL
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)

Share

About the Author

Nicholas Butler
United Kingdom United Kingdom

I built my first computer, a Sinclair ZX80, on my 11th birthday in 1980.
In 1992, I completed my Computer Science degree and built my first PC.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing concurrent software professionally, using multi-processor machines, since 1995.

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.

If you would like help with multithreading, please contact me via my website:


I can work 'virtually' anywhere!

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160530.1 | Last Updated 11 May 2008
Article Copyright 2008 by Nicholas Butler
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid