concept of thread

Download Concept of thread

If you can't read please download the document

Upload: munmun-das-bhowmik

Post on 20-Jun-2015

83 views

Category:

Documents


0 download

TRANSCRIPT

  • 1. Concept of ThreadMunmun Das Bhowmik

2. Objectives Resource sharing Performance Parallel computing 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 & privatestorage area are known as context of thread. 4. What is a Thread ?light-weight processis the smallest sequence of programmed instructions thatcan be managed independently by an operating systemscheduler 5. Benefits : Resourse sharing : On a single-processor machine, theoperating system is switching rapidly between the threads,giving the appearance of simultaneous execution. Responsiveness : Maintain a responsive user interfacewhile background tasks are executing Distinguish tasks of varying priority Economy : Perform operations that consume a largeamount of time without stopping the rest of the application. 6. Another example is a web server - Multiple threads allowfor multiple requests to be satisfied simultaneously,without having to service requests sequentially or to forkoff separate processes for every incoming request. ( Thelatter is how this sort of thing was done before theconcept of threads was developed. A daemon wouldlisten at a port, fork off a child for every incoming requestto be processed, and then go back to listening to theport. ) 7. User & Kernel threads : User threads : User threads are scheduled in user space by theprocess itself. Usually these are provided as afeature of a computer language. Kernel threads : These can be scheduled across different CPUs ina multiprocessor system Kernel threads interact intelligently with the I/Osubsystems of the Kernel. This means that whileone of your threads waits on I/O, the others willcontinue to run 8. Models : User thread(N:1) :Kernel thread (1:1): Hybrid (M:N) :user library N number of application simplest possibleno kernel threads onto some M number threading implementation modificationsof 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 32process, suitable forchanges 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 threadshas benefits ? Which type of thread is used to create new tab in webbrowser ? What type of threads are handled by device drivers ? What is hardware virtualization ? 10. Threads in C# libraryNamespace : System.ThreadingStates :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. SynchronizationBlock methods : Sleep , Wait, Task.WaitLock Constructs :ConstructPurposeCross processLock(Monitor.Enter/only one thread can access a resource-Monitor.Exit)or enter the critical zoneMutex yesSemaphoreSlim (.Net 4.0) Not more than a specified number of- threads can access a resource or enterSemaphorethe critical zoneyesReaderWriterLockSlim (.Net Allows multiple readers to coexist -3.5) with a single writerReaderWriterLock- 14. Protecting shared data in safer way Thread safety concept :wait()T1 Shared data T2ExclusiveT3 Sleep() lockWhile waiting on a Sleep or Join, a thread is blockedand so does not consume CPU resources. 15. Points to consider while designingThreading has resource requirements & potentialconflicts. Few threads should be used as it will minimize theoperating system resources & improving performance. Memory consumed for context those required byAppDomain objects , processes & threads. Consumes processor time. If most of current threads arein one process, then other threads processes arescheduled less frequently. Thread pooling / thread spawning Shared access to resources creates conflicts. Failure to synchronize causes deadlocks or raceconditions. 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 applicationsin .Net ? Explicit thread class , task parallel library , lambdafunctions , BeginInvoke , BackgroundWorker 17. Thank you !