|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionSummaryThis document describes the details of a customized .NET This provides a helper class that simplifies the effort of programmers who are involved in high end programming using multithreading in the Microsoft® .NET Framework. This article also explains how to synchronize access to a shared resource among multiple threads. This guides you to:
This assumes that you are familiar with .NET component development and the basic principles of threading mechanisms. Contents
Shared resource access in multithreading, an overviewA .NET application can perform multiple tasks at the same time by using multithreading. Multithreading permits you to start different threads to complete different tasks at the same time. Multithreading also improves the performance and the responsiveness of applications. Since multiple threads can access any resource at the same time, it is best to synchronize individual threads with the other parts of the program. When an application is working in a multi threaded environment, it needs to maintain that no thread leaves the object in an invalid state when it gets suspended. Thread safety basically means, the members of an object always maintain a valid state when used concurrently by multiple threads. .NET provides different synchronization mechanisms that maintain thread safety in multithreading programming.
Sample code:lock(obj) { // code to be locked will go here }
Sample code:lock(obj) { // code to be locked will go here }
You can replace the code with try
{
Monitor.Enter(obj);
{
// code to be locked will go here
}
finally
{
Monitor.Exit(obj);
}
Sample code:Mutex objMutex = new Mutex(false, "ThreadLock" );
objMutex.WaitOne();
// code to be locked will go here
objMutex.ReleaseMutex();
Design and implementation of ThreadLockHelper class
using System;
using System.Threading;/// <summary>
/// A static class used to lock the managed/unmanaged .net resources
/// </summary>
public class ThreadLockHelper
{
static ThreadLockHelper mInstance = null;
Mutex mMutex = null;
private ThreadLockHelper ()
{
}
public static ThreadLockHelper GetInstance()
{
if( mInstance == null )
{
mInstance = new ThreadLockHelper ();
mInstance.mMutex = new Mutex(false, "ThreadLock" );
}
return( mInstance );
}
public bool CreateLock()
{
if ( mMutex == null )
{
mMutex = new Mutex(false, "ThreadLock");
}
return( mMutex.WaitOne() );
}
public void ReleaseLock()
{
mMutex.ReleaseMutex();
}
}
Sample applications using ThreadLockHelper classCreate a public class Activity
{
public void InvokeTask()
{
Task objTask = new Task();
ThreadLockHelper.GetInstance().CreateLock();
objTask.DoTask();
ThreadLockHelper.GetInstance().ReleaseLock();
}
}
Here If you are invoking the above Activity objActivity = null;
Thread thdInvokeTask ;
for(int i=1; i < 100 ; i++)
{
objActivity = new Activity();
thdInvokeTask = new Thread(new ThreadStart(objClsThread.InvokeTask));
thdInvokeTask.Start();
}
In the above scenario, if you are not applying locking mechanism, your application will fail with thread abort exception. You can test the scenario by implementing a web service invocation in ConclusionThis article gives you information related to effective management of thread synchronization. It also provides you to go through a custom
|
|||||||||||||||||||||||||||||||||||