This document discusses the concept of threads. It defines a thread as the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler. Threads allow for parallel computing and resource sharing. The benefits of threads include maintaining responsiveness, prioritizing tasks, and performing long operations without stopping other tasks. Threads can be user threads scheduled in user space or kernel threads scheduled across CPUs. Common threading models include user threads (N:1), kernel threads (1:1), and hybrid models (M:N). The document also discusses threading in C# and strategies for safely sharing data between threads to avoid issues like race conditions.
3. 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.
4. What is a Thread ?
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 & 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
8. 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
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 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
12. 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.
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 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.
15. 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.
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