Tuesday, February 28, 2012

Multithreading in C# - Basics

·         Multithreading – is the parallel execution of code
·         Use of multithreading
1. Maintaining a responsive UI
2. Allow requests to process simultaneously

·         Joinwait for the thread to end/complete the execution
·         Sleeppauses the current thread for specified period
·         IsAlive – property returns true if thread is alive

Note: Thread will be in blocked state while waiting on a join/sleep. And will not consume resources

·         Yeild – pauses thread running on same processor.
·         Preempted – a thread is said to be preempted when its execution is interrupted due to external factor
·         Interrupt and abort – interrupt will wait for until thread block to complete, while abort breaks where it is executing

·         Thread scheduler manages multithreading
1. It’s a function typically CLR delegates to Operating System
2. TS ensures all active threads are allocated appropriate execution time
3. And threads are waiting/blocked do not consume CPU resource
4. On  a Single processor system, TS perform time-slicing
5. On a multi processor system, TS perform mixture of time slicing and concurrency

·         Process Vs Thread – a thread in execution is a process. Threads run parallel within a single process. Processes are fully isolated from each other, where threads are partially isolated from each other. (threads share heap memory with other threads)

·         Foreground and background threads – by default foreground threads are created. (Thread.IsBackground=true)

·         Thread Priority – property determines how much execution time it gets (Lowest, BelowNormal, Normal, AboveNormal, Highest)

·         Thread pooling –allow execution of the threads simultaneously.   Once the limit is reached it queues up and continues execution when other finishes (ThreadPool.QueueUserWorkItem)

·         Asynchronous delegate – If a thread wants to notify (return value back) after it has finished execution, then this can be achieved using asynchronous delegate.

1. Create a delegate for the method to be called (Func delegate)
2. Call BeginInvoke on delegate with IAsyncResult return value
Note: BeginInvoke return immediately to the caller.

·         ThreadState: gives you threads execution status.
o   Unstarted
o   Running
o   WaitSleepJoin
o   Stopped

·         Thread synchronization
o   Lock (Monitor.Enter, Monitor.Exit)
o   Mutex
o   Semaphore
o   SemaphoreSlim
o   SpinLock

·         Lock – ensure only one thread can enter particular section of code at a time (Exclusive lock)
object locker = new object();
                 System.Threading.Monitor.Enter(locker);

           
                 System.Threading.Monitor.Exit(locker);
·         Mutex – is an exclusive lock. Purpose is inter-process synchronization. It can work across multiple processes. Mutex can be computer-wide or application-wide.

·         Semaphore – Ensures not more than a specified number of concurrent threads can access a resource. It is cross process. 


No comments: