Unformatted text preview:

Samyukta Mudugal CSCI 5448 – Spring 2011 C# THREADS 3/30/2011 13/30/2011 2 Outline • Introduction to threads. • Advantages and Disadvantages of using threads • C# support for threading • Creating threads in C# • Using Thread Class • Using Thread Pool • Synchronization of threads • Blocking • Simple Locking • Mutex • Semaphores • Deadlocks • Aborting ThreadsThread • A thread is an independent execution path with its own resources allocated by the CPU. • Threads run in parallel within a process just as processes run in parallel on a computer. • A process can have multiple threads and they share memory among them. • Each thread had its own context of execution. • The meaning of context differs from one OS to the other. • It could be any of these – registers, memory maps, tables, lists etc. 3/30/2011 3Magic of Multithreading • Achieve true parallelism in a multiprocessor environment. • The threads can execute in parallel on different processors. • Increasing the responsiveness of a user interface. • By running a parallel “worker” thread, the main UI thread is free to continue processing keyboard and mouse events. • Achieve better use of an otherwise blocked CPU. • While one thread waits for a response from a hardware or another computer, the other threads can continue execution. • Distinguish tasks of varying priority. • A high-priority thread manages time-critical tasks, and a low-priority thread performs other tasks. 3/30/2011 4Cost of Multithreading • System could get complex! • CPU costs – scheduling, managing, switching, creation/tear down. • Instead of making your system faster it could in turn make it slower! • Controlling the shared access of resources. • Using the synchronization objects appropriately around the shared resources. • IF synchronization is not done properly it could lead to problems such as deadlocks, race conditions. • These issues are extremely hard to debug. • Testing multithreaded applications is harder. • Defects are often timing-related and more difficult to reproduce. 3/30/2011 5Threading in C# • Import the following namespaces to support threading - using System; using System.Threading; • C# client program starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. • C# provides a ‘thread’ class that can be used to create a thread, control a thread, set its priority, get its status, etc. • C# also provides a pool of threads to which tasks can be assigned. 3/30/2011 6Thread State Machine • A thread can be in one of the major states shown below 3/30/2011 7Creation • C# supports 2 methods to create threads • Using the Thread class. • Using the Thread pool. • Thread Class: A new thread object is created and a delegate is passed to the thread’s constructor. • A thread ends when the delegate passed to the Thread’s constructor finishes executing. • Thread Pool: C# provides a pool of threads. Tasks can be assigned to the threads in the pool. This gets rid of the overhead of creation, deletion and managing the threads. 3/30/2011 8Using the Thread Class 3/30/2011 9Output: • The Main thread prints Y. • The thread created ‘foo’ prints X. 3/30/2011 10Using the Thread Pool • Initializing the arrays and Manual Reset Events. • Manual Reset Event is an object that allows one thread to signal another thread when something happens. In the case of this code, we use these events to signal the main thread that a work item has been completed. 3/30/2011 11Thread Pool contd.. • Assigning the tasks to the threads in the pool. • Any method that you want to queue up for the thread pool to run needs to take one argument, an object, and return void. • The argument will end up being whatever you passed in as the second argument to the QueueUserWorkItem call. 3/30/2011 12Synchronization • Synchronization refers to coordinating the actions of threads for a predictable outcome. • Synchronization is particularly important when threads access the same data. • 4 different kinds of synchronization constructs: • Simple Blocking methods • Locking Constructs • Signaling Constructs • Non Blocking Synchronization Constructs 3/30/2011 13Blocking • Sometimes a thread might have to pause until a certain condition is met. It could either spin or block. • A blocked thread immediately yields its processor time slice, and from then on consumes no processor time until its blocking condition is satisfied. • Spinning is wasteful on processor time because time and resources are allocated accordingly even though the thread is just waiting. • Some methods used for this are: Sleep, Join and Task.Wait. • The ThreadState property is used to check if the thread is blocked. 3/30/2011 14Simple Locking • 3 main exclusive locking constructs are locks, mutexes and semaphores. • Only one thread can lock the synchronizing object (myLock) at a time, and any contending threads are blocked until the lock is released. • If more than one thread contends the lock, they are queued on a “ready queue” and granted the lock on a first-come, first-served basis. 3/30/2011 15Mutex • Mutexes can span across multiple processes in a system. • To lock the mutex: WaitOne method • To unlock the mutex: ReleaseMutex method 3/30/2011 16Semaphores • Semaphores limit the number of threads that can access a resource concurrently. The maximum number of threads is called the capacity of the semaphore. • They work by keeping a counter. • Each time a thread obtains the semaphore the counter is incremented and once the semaphore is released the counter is decremented. • A semaphore with a capacity of one is same as a mutex or a lock. • Any thread can call lock or release on a semaphore but with a mutex, only the thread that obtained it can release it. • Semaphores can be useful in preventing too many threads executing the same piece of code at once. 3/30/2011 17Semaphores contd.. 3/30/2011 18Deadlocks • A deadlock happens when two threads each wait for a resource held by the other, so neither can proceed. • Example of deadlock: • Thread 1 acquires lock A. • Thread 2 acquires lock B. • Thread 1 attempts to acquire lock B, but it is already held by Thread 2 and thus Thread 1 blocks until B is released. • Thread 2


View Full Document

CU-Boulder CSCI 5448 - C# THREADS

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download C# THREADS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C# THREADS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C# THREADS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?