Sunday, August 22, 2010

Threading

Threading is a very wast and interesting area in programming. Without actual experience talking about threading is some what not possible. It requires actual implementation.

.NET supports Threading from the beginning and enhances it in each release. Sytem.Threading is the class dedicated for this.

We can create threads for parameter less and parametrized functions. It is very easy in .NET

Threading.Thread th = new Threading.Thread(fn);
th.Start();
//th.Start(1); //this is for parametrized

public void fn()
{
}
public void fn(object val)
{
}

Synchronization
While dealing with Data and thread, sync is very much important. We can use the below methodologies.
Monitor.Enter - It is a static method and Monitor.Exit should be called in the finally block
lock / synclock - this works same as Monitor.Exit but automatically releases the lock if the scope is out.
ReaderWriterLock - It works best where most accesses are reads, while writes are infrequent and of short duration. Multiple readers alternate with single writers, so that neither readers nor writers are blocked for long periods.
ReaderWriterLock.AcquireReaderLock, ReaderWriterLock.AcquireWriterLock, ReaderWriterLock.UpgradeToWriterLock, ReaderWriterLock.DowngradeFromWriterLock, ReaderWriterLock.ReleaseLock
ReaderWriterLockSlim - It is the slimmer version of ReaderWriterLock and the performance is significantly better than the former.

No comments:

Post a Comment