Concept of Thread




Munmun Das Bhowmik
Objectives




● Resource sharing
● Performance

● Parallel computing
Few terms to be used :

●   Processor : runs a code stream
●   Function : a particular code stream
●   Schedule : time slice given by processor
●   Process : instance of a program being executed
●   Registers : physical storage of CPU
●   Kernel : interface between hardware & software
●   Thread : an execution context for processor
●   Thread context : The register set, stacks & private
    storage area are known as context of thread.
What is a Thread ?
light-weight process
is the smallest sequence of programmed instructions that
can be managed independently by an operating system
scheduler
Benefits :



•   Resourse sharing : On a single-processor machine, the
    operating system is switching rapidly between the threads,
    giving the appearance of simultaneous execution.
•   Responsiveness : Maintain a responsive user interface
    while background tasks are executing
•   Distinguish tasks of varying priority
•   Economy : Perform operations that consume a large
    amount of time without stopping the rest of the application.
•   Another example is a web server - Multiple threads allow
    for multiple requests to be satisfied simultaneously,
    without having to service requests sequentially or to fork
    off separate processes for every incoming request. ( The
    latter is how this sort of thing was done before the
    concept of threads was developed. A daemon would
    listen at a port, fork off a child for every incoming request
    to be processed, and then go back to listening to the
    port. )
User & Kernel threads :

●   User threads :
    ●   User threads are scheduled in user space by the
        process itself. Usually these are provided as a
        feature of a computer language.
●   Kernel threads :
    ●   These can be scheduled across different CPU's in
        a multiprocessor system
    ●   Kernel threads interact intelligently with the I/O
        subsystems of the Kernel. This means that while
        one of your threads waits on I/O, the others will
        continue to run
Models :




 User thread(N:1) :    Kernel thread (1:1):     Hybrid (M:N) :
  user library
                                                ●
                                                 N number of application
 ●                     ●
                        simplest possible
  no kernel                                     threads onto some M number
 ●
                       threading implementation
 modifications                                  of kernel entities, or "virtual
                       ●
                        scheduling/             processors."
 flexible & low cost   synchronization
 ●
  thread may block
                                                ●
                                                 are more complex to
                       coordination             implement than either kernel
 entire process, no
 parallelism
                       ●
                         less overhead than     or user threads, because
 ●
  Eg : Win 32          process, suitable for    changes to both kernel and
                       parallel application     user-space code are required
                                                ●
                                                 Eg : Windows 7
Questions ?

●   What happens when too many user threads are created ?
●   Does all sequential process converted to mutiple threads
    has benefits ?
●   Which type of thread is used to create new tab in web
    browser ?
●   What type of threads are handled by device drivers ?
●   What is hardware virtualization ?
Threads in C# library
Namespace : System.Threading
States :




  The ThreadState property is useful for diagnostic purposes.
C# supports parallel execution of code through multithreading

 ●   A C# client program (Console, WPF, or Windows Forms) starts
     in a single thread created automatically by the CLR and
     operating system (the “main” thread), and is made multithreaded
     by creating additional threads.




 ●   WCF, ASP.NET, and Web Services applications are
     implicitly multithreaded
If threads are so good, why not use them everywhere?



   ●
       When mutiple threads tries to access shared data
   ●
       Concurrent access to variables
   ●
       To avoid race conditions : .This is a scenario where
       two or more threads are competing(racing) to
       read/write from a shared memory location leading to
       the possibilities of leaving the program in an
       inconsistent state.
Synchronization
Block methods : Sleep , Wait, Task.Wait
Lock Constructs :
Construct                    Purpose                                  Cross process

Lock(Monitor.Enter/          only one thread can access a resource    -
Monitor.Exit)                or enter the critical zone

Mutex                                                                 yes

SemaphoreSlim (.Net 4.0)     Not more than a specified number of      -
                             threads can access a resource or enter
Semaphore                    the critical zone                        yes


ReaderWriterLockSlim (.Net   Allows multiple readers to coexist       -
3.5)                         with a single writer

ReaderWriterLock                                                      -
Protecting shared data in safer way

●   Thread – safety concept :


                    wait()
              T1                       Shared data
                   T2      Exclusive
              T3
                   Sleep()   lock



  While waiting on a Sleep or Join, a thread is blocked
and so does not consume CPU resources.
Points to consider while designing
    Threading has resource requirements & potential
    conflicts.
●   Few threads should be used as it will minimize the
    operating system resources & improving performance.
●   Memory consumed for context those required by
    AppDomain objects , processes & threads.
●   Consumes processor time. If most of current threads are
    in one process, then other threads processes are
    scheduled less frequently.
●   Thread pooling / thread spawning
●   Shared access to resources creates conflicts.
●   Failure to synchronize causes deadlocks or race
    conditions.
Questions :

●   Benefits of thread pooling ?
●   How to handle long running threads ?
●   What about the performance ?
●   What is the mechanism for Worker thread ?
●   Different ways to create muti-threaded applications
    in .Net ?
    ●   Explicit thread class , task parallel library , lambda
        functions , BeginInvoke , BackgroundWorker
Thank you !

Concept of thread

  • 1.
  • 2.
    Objectives ● Resource sharing ●Performance ● Parallel computing
  • 3.
    Few terms tobe used : ● Processor : runs a code stream ● Function : a particular code stream ● Schedule : time slice given by processor ● Process : instance of a program being executed ● Registers : physical storage of CPU ● Kernel : interface between hardware & software ● Thread : an execution context for processor ● Thread context : The register set, stacks & private storage area are known as context of thread.
  • 4.
    What is aThread ? light-weight process is the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler
  • 5.
    Benefits : • Resourse sharing : On a single-processor machine, the operating system is switching rapidly between the threads, giving the appearance of simultaneous execution. • Responsiveness : Maintain a responsive user interface while background tasks are executing • Distinguish tasks of varying priority • Economy : Perform operations that consume a large amount of time without stopping the rest of the application.
  • 6.
    Another example is a web server - Multiple threads allow for multiple requests to be satisfied simultaneously, without having to service requests sequentially or to fork off separate processes for every incoming request. ( The latter is how this sort of thing was done before the concept of threads was developed. A daemon would listen at a port, fork off a child for every incoming request to be processed, and then go back to listening to the port. )
  • 7.
    User & Kernelthreads : ● User threads : ● User threads are scheduled in user space by the process itself. Usually these are provided as a feature of a computer language. ● Kernel threads : ● These can be scheduled across different CPU's in a multiprocessor system ● Kernel threads interact intelligently with the I/O subsystems of the Kernel. This means that while one of your threads waits on I/O, the others will continue to run
  • 8.
    Models : Userthread(N:1) : Kernel thread (1:1): Hybrid (M:N) : user library ● N number of application ● ● simplest possible no kernel threads onto some M number ● threading implementation modifications of kernel entities, or "virtual ● scheduling/ processors." flexible & low cost synchronization ● thread may block ● are more complex to coordination implement than either kernel entire process, no parallelism ● less overhead than or user threads, because ● Eg : Win 32 process, suitable for changes to both kernel and parallel application user-space code are required ● Eg : Windows 7
  • 9.
    Questions ? ● What happens when too many user threads are created ? ● Does all sequential process converted to mutiple threads has benefits ? ● Which type of thread is used to create new tab in web browser ? ● What type of threads are handled by device drivers ? ● What is hardware virtualization ?
  • 10.
    Threads in C#library Namespace : System.Threading States : The ThreadState property is useful for diagnostic purposes.
  • 11.
    C# supports parallelexecution of code through multithreading ● A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. ● WCF, ASP.NET, and Web Services applications are implicitly multithreaded
  • 12.
    If threads areso good, why not use them everywhere? ● When mutiple threads tries to access shared data ● Concurrent access to variables ● To avoid race conditions : .This is a scenario where two or more threads are competing(racing) to read/write from a shared memory location leading to the possibilities of leaving the program in an inconsistent state.
  • 13.
    Synchronization Block methods :Sleep , Wait, Task.Wait Lock Constructs : Construct Purpose Cross process Lock(Monitor.Enter/ only one thread can access a resource - Monitor.Exit) or enter the critical zone Mutex yes SemaphoreSlim (.Net 4.0) Not more than a specified number of - threads can access a resource or enter Semaphore the critical zone yes ReaderWriterLockSlim (.Net Allows multiple readers to coexist - 3.5) with a single writer ReaderWriterLock -
  • 14.
    Protecting shared datain safer way ● Thread – safety concept : wait() T1 Shared data T2 Exclusive T3 Sleep() lock While waiting on a Sleep or Join, a thread is blocked and so does not consume CPU resources.
  • 15.
    Points to considerwhile designing Threading has resource requirements & potential conflicts. ● Few threads should be used as it will minimize the operating system resources & improving performance. ● Memory consumed for context those required by AppDomain objects , processes & threads. ● Consumes processor time. If most of current threads are in one process, then other threads processes are scheduled less frequently. ● Thread pooling / thread spawning ● Shared access to resources creates conflicts. ● Failure to synchronize causes deadlocks or race conditions.
  • 16.
    Questions : ● Benefits of thread pooling ? ● How to handle long running threads ? ● What about the performance ? ● What is the mechanism for Worker thread ? ● Different ways to create muti-threaded applications in .Net ? ● Explicit thread class , task parallel library , lambda functions , BeginInvoke , BackgroundWorker
  • 17.