Threading - Basic


How can we describe a thread ?
  •  Thread is a single execution path must have entry point, exit point and must have described.
  •  You can call thread as a data structure and data type of the same is System.Threading.Thread.
  •  We have two types of threading model in Windows.
  • In OS level, we have physical threading model.
  • In virtual machines , we have logical threading model. CLR works on logical threading model.
  • It might be 1:1 or 1:n relationship of Physical thread with the CLR thread. i.e. Every logical thread will be owned by single Physical thread.
  • Windows thread uses one data type Handle unlike its counterpart Linux which uses different type for each object.
  • If you imagine windows is doing lot of things simultaneously in one process and the reason for that is thread.
  • Then what is the use of logical thread ? Abstraction and more features are advantages.


Then where do we use threads in application?

  • In EPBX, the ISDN lines receive n calls simultaneously and you have to accept or reject the calls based on condition. Threads are created to work on those simultaneous calls and handle the call efficiently. Here thread is a non-blocking listener.
  • In UI applications UI will be accessed by a single thread where thread is a non-blocking UI.
  • Timer is also a thread.
Above are only few practical scenarios to understand threads. Threads are used in each and every application one or the other way.

How threads work?  

  • Threads work in either cooperative multi-tasking way or preemptive way.
  • Cooperative multi-tasking works on the basis of mutual understanding. If one thread will be running, it will notify other signals about taking resource. But here there can be some threads which have to wait for longer duration.
  • Pre-emptive multi-tasking works on time slice. Every thread will run for specific time slice and will give resource to other thread. And this process will be repeated for working threads.
  • Preemptive multi-tasking is more efficient since there is no starvation or waiting period will be less.

How these threads are managed?
  •  There are 2 types of threads in this regard. Explicit Threads and Thread pool threads.
  •   Programmers take the responsibility of managing threads in case of explicit threads and are foreground threads.
  • CLR takes the responsibility of thread pool threads and they are background threads. 
  • All foreground threads close will preempt the entire process.
  •  All background threads will close when the entire foreground threads closes.

What is the composition of the Thread?


Some of the important composition of Thread:

  •  Managed thread ID – Every thread will contain one ID as an identity. It is of type integer.

  •  Apartment – (Sync – MT). This property is useful in synchronization. The apartment can be single or multi-threaded apartment. You can refer below link for more info.


  • Culture /UI Culture – This tells you about the language and other globalization features.
  • State – Below link gives more information about the thread state.

  • Name – Name of the thread. 
  • Priority – Priority in which it has to be run.
  • IsBackground – Whether it is a background thread? 
  •  IsThreadPoolThread – Checks whether the thread is a thread pool thread or explicit.


How to start a Thread?
  • Every thread will have stack size and entry point.
  • Method is the entry point and defines the exit for boundary. Application needs a function pointer or objects or delegate to point to the method which executes the block of code in thread.
  • There are 2 delegates ThreadStart and ParameterizedThreadStart which takes method as parameter and starts the thread.
 




Beginning with the .NET Framework 4, the behaviour of some thread constructors is changed: Only fully trusted code can set the maximum stack size to a value that is greater than the default stack size (1 megabyte). If a larger value is specified when code is running with partial trust, the larger value is ignored and the default stack size is used. No exception is thrown. Code at any trust level can set the maximum stack size to a value that is less than the default stack size. (Excerpts from MSDN)
Refer https://msdn.microsoft.com/en-us/magazine/cc163990.aspx for more understanding of trusted codes.




Concurrency and synchronization are 2 important properties of thread we should look into while using threads.



Concurrency is nothing but simultaneous execution of threads within the process space.

Consider a situation where 2 threads simultaneously want to execute the threads. Consider the case of transactions in case of joint account. Suppose 2 people simultaneously withdrawing the money, the balance amount is a shared resource. It has to be given access by only one thread at a time. This process of protecting the shared resource from simultaneous execution in multi-threaded environment is known as Synchronization.



Then what is the relationship between concurrency and synchronization. As the level of synchronization increases, the degree of concurrency is decreased and chances of dead lock are more.

We can go through deadlock in subsequent discussions. 




Since synchronization is required only in some cases where shared resources are used, we can have two types of thread programming.
             Synchronous thread
             Asynchronous thread (We will be taking care this topic in different post)

Synchronization can be achieved in many ways. Most important is monitor and wait handle.


[System.Runtime.CompilerServices.MethodImpl] attribute can be set as parameter to method block if you want the method to be accessed by a single thread at a time.
Synchronous and Asynchronous thread programming will be covered in detail in next post. Even you might find some information about advanced thread level programming in C#.

No comments:

Post a Comment

Pages