C# Multithreading

Multithreading defines multiple threads which execute at the same time. As we know thread is known as the execution path for the program. Multithreading in C# is known as a process in which multiple threads work at the same time. Multithreading is used to achieve multi-tasking. Multi-Tasking saves time by executing multiple tasks at once. Multithreading is created by using System.Threding namespace. Multithreading saves wastage of CPU cycle and increases the efficiency of an application.

Life Cycle of Thread:

There are multiple phases in thread life cycle. It starts with the object is created and ends when the thread is terminated.

Given below are the phases of Thread life cycle.

  1. The Unstarted State
  2. The ready state
  3. The not runnable state
  4. The dead state
1- The unstarted State:

In this state the instance of the thread is created but not started yet.

2- The ready state:

In this state the thread is ready to run and waiting for CPU cycle.

3- The Not Runnable State:

There are some states in which a thread is not runnable. Such as sleep method or wait method has been called. Blocked by I / O operations.

4- The Dead State:

In this state thread completes execution and or is aborted.

Properties of Thread:

Following are the properties of thread:

Properties Description
CurrentCulture This property sets the culture for the current thread.
CurrentContextThis property gets the current context where the thread is executing.
CurrentThread This property gets the currently running thread.
IsBackground This property sets a value indicating whether or
not a thread is a background thread.
CurrentUICultureThis property sets the current culture used by the
Resource Manager to look up culture-specific resources at run-time
CurrentPrincipleThis property sets the thread’s current principal (for role-based security).
IsAliveThis property gets a value indicating the execution status of the current thread.
IsThreadPoolThreadThis property gets a value indicating whether or
not a thread belongs to the managed thread pool.
IsBackgroundThis property sets a value indicating whether
or not a thread is a background thread.
ThreadStateThis property gets a value containing the states of the current thread
NameThis property gets or sets the name of the thread.
ManagedThreadIdThis property gets a unique identifier for the current managed thread.
PriorityThis property gets or sets a value indicating
the scheduling priority of a thread.

Methods of Thread:

Following are the methods of thread:

Methods Description
public static LocalDataStoreSlot AllocateDataSlot()This method allocates an unnamed data slot on all the threads. It uses fields that are marked with the ThreadStaticAttribute attribute instead.
public static void EndCriticalRegion() This method notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception are limited to the current task.
public static void BeginCriticalRegion() This method notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.
public static LocalDataStoreSlot AllocateNamedDataSlot(string name)Allocates a named data slot on all threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
public static Object GetData(LocalDataStoreSlot slot)Retrieves the value from the specified slot on the current thread, within the current thread’s current domain. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
public static void EndThreadAffinity()Notifies a host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread.
public static void FreeNamedDataSlot(string name)Eliminates the association between a name and a slot, for all threads in the process. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
public static LocalDataStoreSlot GetNamedDataSlot(string name)Looks up a named data slot. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
public static void SetData(LocalDataStoreSlot slot,
Object data)
Sets the data in the specified slot on the currently running thread, for that thread’s current domain. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
public static bool Yield()Causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to.
public void Join()Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping. This method has different overloaded forms.
public static AppDomain GetDomain()Returns the current domain in which the current thread is running.
public static void MemoryBarrier()Synchronizes memory access as follows: The processor executing the current thread cannot reorder instructions in such a way that memory accesses prior to the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier.
public void Interrupt()Interrupts a thread that is in the WaitSleepJoin thread state.
public static byte VolatileRead(ref byte address)public static double VolatileRead(ref double address)public static int VolatileRead(ref int address)public static Object VolatileRead(ref Object address)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache. This method has different overloaded forms. Only some are given above.
public static void Sleep(int millisecondsTimeout)Makes the thread pause for a period of time.
public void Start()Starts a thread.
public static void ResetAbort()Cancels an Abort requested for the current thread.
public static void SpinWait(int iterations)Causes a thread to wait the number of times defined by the iterations parameter
public static void VolatileWrite(ref byte address,byte value)public static void VolatileWrite(ref double address, double value)public static void VolatileWrite(ref int address, int value)public static void VolatileWrite(ref Object address, Object value)Writes a value to a field immediately, so that the value is visible to all processors in the computer. This method has different overloaded forms. Only some are given above.
public static AppDomain GetDomainID()Returns a unique application domain identifier