eleks summer school 2012: .net 06 - multithreading
DESCRIPTION
"Multithreading" lecture @ ELEKS Summer School 2012 (Jul 2012)TRANSCRIPT
![Page 2: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/2.jpg)
Summer School 2012
Although threads seem to be a small step from sequential
computation, in fact, they represent a huge step.
They discard the most essential and appealing properties
of sequential computation: understandability,
predictability, and determinism.
Threads, as a model of computation, are wildly
nondeterministic, and the job of the programmer
becomes one of pruning that nondeterminism.
Edward A. Lee, UC Berkeley, 2006
“
”
![Page 3: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/3.jpg)
History: so why did we need threads?
Summer School 2012
• CPU virtualization• Increased robustness• Quazi-multitasking
![Page 4: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/4.jpg)
Process as a resource isolation unit
Summer School 2012
![Page 5: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/5.jpg)
Concurrent executiondoes not exist
on a single logical CPU!
Summer School 2012
![Page 6: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/6.jpg)
Context Switching
Summer School 2012
![Page 7: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/7.jpg)
Scheduling• Processes are not scheduled. Threads are.• 32 different thread priority levels!
Summer School 2012
1. Idle2. Below
Normal3. Normal4. Above
Normal5. High6. Realtime
1. Idle2. Lowest3. Below
Normal4. Normal5. Above
Normal6. Highest7. Time-Critical
ProcessThread
![Page 8: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/8.jpg)
Thread Overhead• Thread Kernel Object• Thread Environment Block (TEB)• User-Mode Stack [1 MB]
• Kernel-Mode Stack [12 / 24 KB]
• DLL: Attach/Detach Notifications
Summer School 2012
![Page 9: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/9.jpg)
Moore’s Law
Summer School 2012
![Page 10: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/10.jpg)
Threads for parallelization?
Summer School 2012
![Page 11: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/11.jpg)
Multiple Logical CPUs
Summer School 2012
• Multi-socket motherboards• Single-core CPUs with Hyper Threading
(HT)• Multi-core CPUs• Multi-core CPUs with per-core HT
![Page 12: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/12.jpg)
Thread Operations
Summer School 2012
I/O-boundCompute-bound
![Page 13: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/13.jpg)
Compute-bound Operations
Summer School 2012
static void Main(string[] args) { Thread workerThread = new Thread(Factorial); workerThread.Start(10); workerThread.Join(); Console.ReadKey(true); } static void Factorial(object arg) { int n = (int)arg; int result = 1; for (int i = 2; i <= n; i++) result *= i; Console.WriteLine(result); }
![Page 14: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/14.jpg)
Thread Lifecycle
Summer School 2012
![Page 15: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/15.jpg)
Too many threads = too bad
Summer School 2012
• Kernel object overhead• Memory overhead• Context switch overhead• GC overhead
Use threads wisely!
![Page 16: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/16.jpg)
Thread Pool
Summer School 2012
• Designed for background processing• Self-balancing• Avoids much overhead
![Page 17: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/17.jpg)
Thread Pool: Compute-bound
Summer School 2012
static void Main(string[] args) { ThreadPool.QueueUserWorkItem(Factorial, 10); Console.ReadKey(true); }
static void Factorial(object n) { int x = (int)n; int result = 1; for (int i = 2; i <= x; i++) result *= i; Console.WriteLine(result); }
![Page 18: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/18.jpg)
Self-Balancing
Summer School 2012
Too few threads:Allocate more
Too many threads:Remove excessive ones
![Page 19: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/19.jpg)
Task-Based Threading
Summer School 2012
“Work Stealing” Principle!
![Page 20: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/20.jpg)
Tasks: Compute-bound
Summer School 2012
static void Main(string[] args) { Task<Int32> task = new Task<Int32>(Factorial, 10); task.Start(); task.Wait(); Console.WriteLine("The Sum is: " + task.Result); Console.ReadKey(true); }
static int Factorial(object n) { int x = (int)n; int result = 1; for (int i = 2; i <= x; i++) result *= i; return result; }
![Page 21: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/21.jpg)
Tasks 101
Summer School 2012
• Each task can have child tasks…• …and, therefore, throw multiple exceptions• “Task” does not mean “separate thread”!• Task templating via TaskFactory • Custom scheduling via TaskScheduler • Cooperative cancellation via
CancellationToken• Continuations, WaitAny, WaitAll, …
![Page 22: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/22.jpg)
Tasks: Continuations
Summer School 2012
static void Main(string[] args)
{
Task<Int32> task = new Task<Int32>(Factorial, 10);
task.ContinueWith(t => Console.WriteLine("Completed"),
TaskContinuationOptions.OnlyOnRanToCompletion);
task.ContinueWith(t => Console.WriteLine("Canceled"),
TaskContinuationOptions.OnlyOnCanceled);
task.ContinueWith(t => Console.WriteLine("Error"),
TaskContinuationOptions.OnlyOnFaulted);
task.Start();
}
![Page 23: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/23.jpg)
Parallel LINQ (PLINQ)
Summer School 2012
enumerable.AsParallel()
![Page 24: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/24.jpg)
Parallel Computations via PLINQ
Summer School 2012
static void Main(string[] args) { Parallel.For(1, 10, n => Console.WriteLine("{0}: {1}", n, Factorial(n))); Enumerable.Range(1, 9).AsParallel().ForAll(n => /* Console... */); new int [] { 1, 2, 3 }.AsParallel().ForAll(n => /* Console... */); }
![Page 25: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/25.jpg)
Summer School 2012
.AsParallel()
does not mean faster execution
• Possible GC pressure ( i => new { ... } )
• Item acquisition and delegate execution are implemented in virtual methods
• You have to benchmark every particular situation
![Page 26: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/26.jpg)
Synchronous I/O
Summer School 2012
![Page 27: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/27.jpg)
Async I/O
Summer School 2012
![Page 28: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/28.jpg)
Async I/O Benefits
Summer School 2012
• Less threads• Less GC• Faster debugging• Faster I/O if made parallel• Responsive UI (a must for WinPhone &
Silverlight)
![Page 29: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/29.jpg)
Asynchronous Programming Model (APM)
Summer School 2012
Implemented by:
• Streams• Sockets• Serial Ports• SQL Commands• DNS Requests• Web Services …etc.
![Page 30: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/30.jpg)
The Async Model Soup
Summer School 2012
Which one to follow? Stop the madness!
![Page 31: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/31.jpg)
New APM in .NET 4.5 / C# vNext
Summer School 2012
private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (var response = webReq.GetResponse()) using (Stream responseStream = response.GetResponseStream()) responseStream.CopyTo(content); return content.ToArray(); } private async Task<byte[]> GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) using (Stream responseStream = response.GetResponseStream()) await responseStream.CopyToAsync(content); return content.ToArray(); }
![Page 32: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/32.jpg)
Threads and Shared State
Summer School 2012
![Page 33: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/33.jpg)
Threads and Race Conditions
Summer School 2012
![Page 34: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/34.jpg)
Synchronization Constructs
Summer School 2012
User-Mode• Volatile Read/Write• Interlocked
Kernel-Mode• Events• Semaphores• …and everything derived from them
Hybrid• Double-checked locking• Many others…
![Page 35: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/35.jpg)
To make things even worse
Summer School 2012
• Language Compiler optimizations• JIT Compiler optimizations• CPU optimizations – Out-of-order execution – Branch prediction – Memory barriers
![Page 36: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/36.jpg)
Atomic (Interlocked) Operations
Summer School 2012
• Atomic Swap• Test-and-Set• Compare-and-Swap• Fetch-and-Add• Load-Link / Store-Conditional
![Page 37: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/37.jpg)
Kernel-mode Constructs
Summer School 2012
• WaitHandle (base class)• AutoResetEvent• ManualResetEvent• CountdownEvent• Semaphore• Mutex
![Page 38: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/38.jpg)
Hybrid constructs: double-checked locking
Summer School 2012
internal sealed class Singleton { private static readonly Object s_lock = new Object(); private static Singleton s_value = null;
private Singleton() { }
public static Singleton GetSingleton() { if (s_value != null) return s_value; Monitor.Enter(s_lock); if (s_value == null) { Singleton temp = new Singleton(); Interlocked.Exchange(ref s_value, temp); } Monitor.Exit(s_lock); return s_value; }}
![Page 39: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/39.jpg)
Concurrent Collections
Summer School 2012
• BlockingCollection<T>
• ConcurrentBag<T>
• ConcurrentDictionary<K, V>
• ConcurrentQueue<T>
• ConcurrentStack<T>
![Page 40: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/40.jpg)
Building Scalable Applications
Summer School 2012
• Avoid thread blocking• Avoid shared state• Avoid statics• Avoid mutable structures
![Page 42: ELEKS Summer School 2012: .NET 06 - Multithreading](https://reader035.vdocuments.site/reader035/viewer/2022062709/558cecd2d8b42a31128b4599/html5/thumbnails/42.jpg)
Thank you!
Summer School 2012