Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common.

Similar presentations


Presentation on theme: "Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common."— Presentation transcript:

1 Chapter 14 Multithreading Yingcai Xiao

2 Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common language runtime, the fundamental unit of execution is the thread.  A managed application begins its life as a single thread but can spawn additional threads.  Threads running concurrently share the CPU (or CPUs) by using scheduling algorithms provided by the system.  To an observer, it appears as if all the threads are running at once. In reality, they simply share processor time.. Multithreading

3  A single-threaded application uses just one processor at a time. A multithreaded application can have different threads running on different processors.  Windows NT kernel (NT, 2000, XP, Vista, 7, 8) supports multiple processors using a strategy called symmetric multiprocessing (SMP). Versions that derive from Windows 95 (95, 98, Millennium) do not. .NET Framework’s threading API: to start, stop, manipulate, prioritize and synchronize threads.

4 Example using System; using System.Threading; class MyApp { static void Main () { for (int i=0; i<10; i++) { Thread thread = new Thread( new ThreadStart(ThreadFunc)); thread.Name = "My thread # " + i; thread.Start (); }

5 Example static void ThreadFunc () { String name = Thread.CurrentThread.Name; for (int i=0; i<10; i++) { for( int j=0; j<100000000; j++); //work System.Console.WriteLine (name + " running at "+ DateTime.Now); }

6 Multithreading  Starting a thread: 1.Create a thread object. 2.Setup a function for the thread to run. 3.Start the thread. Thread thread = new Thread (new ThreadStart (ThreadFunc)); thread.Start (); 4. Start simply makes the thread eligible to be allotted CPU time. The system decides when the thread begins running and how often it’s accorded processor time. 5. When a thread function returns, the corresponding thread ends.  Alive and Suspend if (thread.IsAlive) { thread.Suspend (); }

7 Foreground Threads vs. Background Threads  An application doesn’t end until all of its foreground threads have ended.  It can, however, end with background threads running.  The IsBackground property defaults to false, which means that threads are foreground threads by default.  Up to the application to determine a thread is foreground or background.  Example, application ends as soon as it’s started because it changes the auxiliary threads from foreground threads to background threads:

8 Foreground Threads vs. Background Threads using System; using System.Threading; class MyApp { static void Main () { for (int i=0; i<10; i++) { Thread thread = new Thread (new ThreadStart (ThreadFunc)); thread.IsBackground = true; thread.Start (); } static void ThreadFunc () { DateTime start = DateTime.Now; while ((DateTime.Now - start).Seconds < 5); }

9 Threads Priorities  Once a thread is started, the amount of processor time it’s allotted is determined by the thread scheduler.  Thread.Priority can influence how much or how little CPU time a thread receives relative to other threads in the same process. Statistical not precise.  Setting a thread to higher priority impacts not only other threads in the same application but also slows down other applications too.  Thread Priorities ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal (the default) ThreadPriority.BelowNormal ThreadPriority.Lowest thread.Priority = ThreadPriority.AboveNormal;

10 Multithreading in Action  Many applications intrinsically have two threads of execution: (1) Computation thread carries on the main computational task. It runs the computational loop. (2) User interface thread waits for user interaction. It may abort the computational thread if the user elect to do so. It runs the event loop.

11 Multithreading in Action  The Sieve and MultiSieve are examples of such applications: a computational component for searching prime numbers and a GUI for user interactions.  The Sieve example is not multi threaded, the user can’t interact with the application whence the computation starts. Examples\C14\Sieve  The MultiSieve example is multi threaded, the user can cancel the computation after starting it and the application can exit in the middle of the computation. Examples\C14\MultiSieve

12 Thread Synchronization  The hard part of designing a multithreaded program is figuring out where concurrently running threads might clash and using thread synchronization logic to prevent clashes (e.g., reading while writing to the same variable) from occurring.  NET Framework provides the synchronization primitives to support applications’ synchronization logic. AutoResetEvent: Blocks a thread until another thread sets the event Interlocked:Enables simple operations such as incrementing and decrementing integers to be performed in a thread-safe manner ManualResetEvent:Blocks one or more threads until another thread sets the event Monitor:Prevents more than one thread at a time from accessing a resource Mutex:Prevents more than one thread at a time from accessing a resource and has the ability to span application and process boundaries ReaderWriterLock:Enables multiple threads to read a resource simultaneously but prevents overlapping reads and writes as well as overlapping writes

13 Thread Pooling  Instead of launching threads yourself, you pass requests to a thread pool manager. The thread pool manager maintains a pool of threads that’s sized as needed to satisfy incoming requests.  NET Framework provides the ThreadPool class as an API to the managed thread pooling.  You must never terminate a pooled thread. The thread pool manager creates pooled threads, and it terminates them, too.  You can use Thread’s IsThreadPoolThread property to determine whether a thread is a pooled thread.

14 Thread Pooling class MyApp { static int count = 0; static void Main () { WaitCallback callback = new WaitCallback (ProcessRequest); // start the threads from the pool to run the callbacks ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); Thread.Sleep (5000); // Give the requests a chance to execute }

15 Thread Pooling static void ProcessRequest (object state) { int n = Interlocked.Increment (ref count); // lock then increment count Console.WriteLine (n); }


Download ppt "Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common."

Similar presentations


Ads by Google