Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Microsoft Virtual Academy

Header
Advanced .NET Threading, Part 1
Thread Fundamentals
Jeffrey Richter

Produced by
Thread Fundamentals

Early OSes didnt 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 threads 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
Thread Fundamentals

Every context switch requires that Windows


Save registers from CPU to running threads kernel object
Determine which thread to schedule next
If thread owned by other process, switch address space
Load registers from selected threads 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
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 wont schedule a waiting thread


In reality, most threads in the system are waiting for something
A look at the system overall & File Open Dialog
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();
}
}
Thread overhead
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
Temporarily Suspending a Thread

Sleep suspends a thread for an approximate time


Avoid this method as it blocks a thread wasting space (not time)
public class Thread {
public static void Sleep(Int32 milliseconds);
}

Use a Timer instead to have a thread pool invoke a callback


method periodically
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
Its typically better to lower a threads priority
Used for long-running compute-bound tasks
Wont adversely affect other processes
You should avoid raising a threads 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
Thread Priorities & System Responsiveness
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 threads 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
Doesnt interrupt foreground app improving responsiveness

You might also like