Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thread Fundamentals Header Advanced .NET Threading, Part 1

Similar presentations


Presentation on theme: "Thread Fundamentals Header Advanced .NET Threading, Part 1"— Presentation transcript:

1 Thread Fundamentals Header Advanced .NET Threading, Part 1
Microsoft Virtual Academy Header Advanced .NET Threading, Part 1 Thread Fundamentals Jeffrey Richter Produced by

2 Thread Fundamentals Early OSes didn’t support threads (there was just 1 thread) Problem: Long-running tasks affected all apps and the OS Solution: Windows supports 1+ threads/process for robustness Threads have space & time overhead Kernel object (contains thread’s properties & register set context) Context size in bytes: x86 = ~700, x64 = ~1240, ARM = ~350 User-mode data (Thread Environment Block) 4KB, exception-handling chain, TLS, GDI/OpenGL stuff Stacks: user-mode (1MB) & kernel-mode (12KB/24KB) DLL thread attach/detach notifications 1 CPU can only run 1 thread at a time After quantum, Windows context switches to another thread

3 Thread Fundamentals Every context switch requires that Windows
Save registers from CPU to running thread’s kernel object Determine which thread to schedule next If thread owned by other process, switch address space Load registers from selected thread’s kernel object into CPU After the switch, CPU suffers cache misses repopulating its cache All of this is pure overhead and hurts performance But required for a robust OS Conclusion Avoid threads: incur time & memory overhead Use threads: responsiveness & scalability (on multi-CPU system) This class is about wrestling with this tension

4 Windows Schedules Threads
All threads “appear” to run simultaneously Windows schedules a thread to each CPU Windows allows a thread to run for a time quantum When quantum expires, Windows performs a context switch A thread can voluntarily end its time quantum early Usually by waiting for input (keyboard, mouse, network, file) Windows won’t schedule a waiting thread In reality, most threads in the system are waiting for something

5 A look at the system overall & File Open Dialog

6 Programming .NET Framework Applications with C#
Creating a Thread using System; using System.Threading; public static class Program { // 1. Define the thread method private static void ThreadMethod(Object state) { // Do whatever here... } public static void Main() { // 2. Construct Thread object Thread t = new Thread(ThreadMethod); // 3. Create a thread and let it run t.Start("Initialization data"); // Do whatever here... // 4. Optional: Wait for thread to die t.Join(); } } Copyright (c) by Wintellect, LLC (Unauthorized duplication prohibited)

7 Thread overhead

8 Reasons to Create Threads
Two reasons to create threads Isolate code from other code: Responsiveness/easier coding Concurrent execution: Scalability on multi-processor machines Benefits Keep the CPU busy User gets more features with no learning curve App reliability (no hangs, ability to cancel, responsive UI) Examples Indexing files for fast searching Defragmenting disk for better I/O speed Building your project when you stop typing Recalculating spreadsheet cells Spell/grammar checking documents

9 Temporarily Suspending a Thread
Sleep suspends a thread for an approximate time Avoid this method as it blocks a thread wasting space (not time) Use a Timer instead to have a thread pool invoke a callback method periodically public class Thread { public static void Sleep(Int32 milliseconds); }

10 Thread Priorities Each thread has priority property
Highest AboveNormal Normal BelowNormal Lowest Windows schedules threads to CPUs from Highest  Lowest until all CPUs are saturated It’s typically better to lower a thread’s priority Used for long-running compute-bound tasks Won’t adversely affect other processes You should avoid raising a thread’s priority Used for tasks that need to react immediately to something, execute for a very short time, and then block Can adversely affect other processes

11 Thread Priorities & System Responsiveness

12 WinRT Threading APIs & Windows Store Apps
Programming .NET Framework Applications with C# WinRT Threading APIs & Windows Store Apps WinRT has a Windows.System.Threading namespace WinRT offers no API to create a dedicated thread or change a thread’s priority Instead, use ThreadPool.RunAsync(WorkItemHandler, WorkItemPriority) WorkItemPriority can be Low/Normal/High WinRT offers no API to put a thread to sleep Instead, use ThreadPoolTimer.Create(Periodic)Timer Background Windows Store apps have all threads suspended Conserves power Doesn’t interrupt foreground app improving responsiveness All I/O operations MUST be done asynchronously .NET APIs: System.Threading Absent: Thread, ThreadPool There: CancellationToken(Source), ReaderWriterLock(Slim) Sync objs: Auto/ManualResetEvent(Slim), Mutex, Semaphore(Slim), Volatile.Read/Write, Interlocked, Monitor, SpinLock Task: ContinueWith, Delay, Run, Wait(All/Any), WhenAll/Any, Yield, Parallel, PLINQ?? Copyright (c) by Wintellect, LLC (Unauthorized duplication prohibited)


Download ppt "Thread Fundamentals Header Advanced .NET Threading, Part 1"

Similar presentations


Ads by Google