310 likes | 407 Views
Concurrent Programming: From Thread Pool to Parallel Extensions. Sasha Goldshtein Senior Consultant, Sela Group http://blogs.microsoft.co.il/blogs/sasha. The Reason We’re Here. I spare you: “The free lunch is over” “Moore’s law has changed”
E N D
Concurrent Programming: From Thread Pool to Parallel Extensions Sasha Goldshtein Senior Consultant, Sela Group http://blogs.microsoft.co.il/blogs/sasha
The Reason We’re Here • I spare you: • “The free lunch is over” • “Moore’s law has changed” • “Amdahl’s law demands shift in software concepts” • I give you: Sasha’s Law Only Parallel Applications Will Survive
There Is No Silver Bullet • Code, rinse, repeat • Patterns are emerging • Frameworks are emerging
So What’s In This Session? • What kind of work likes parallelism? • Threads and thread pool • APM • Synchronization (is hard) • Concurrency architecture and patterns • Upcoming frameworks
The Faces Of Parallelism • Embarrassingly parallel applications: • Calculating prime numbers, predicting weather • (Partially) CPU-bound applications: • Business rule engine, database server • I/O-bound applications: • Web server, file server, web browser
System.Threading 101 • Create a thread (System.Thread) for each user request • Why is this BAD?
Demo Thread per request
Threads Bad; Tasks Good (102) • Task: An independent unit of work • Thinking in tasks decouples us from threads • Scheduling is done by a resource manager • Revised server: ThreadPool.QueueUWI()
Demo Fire-and-forget using System.ThreadPool
What If The Thread Pool Explodes? • There is no throttling: • Arrival rate: 1,000/second • Execution rate: 10/second • What do you think will happen? • We need a bounded thread pool
DEMO Bounded thread pool
We depend on tasks! System.Threading 103 • User clicks a button • Synchronous operation = UI not responsive • Fix: Execute the request asynchronously and (somehow) get the response later
Demo The ubiquitous APM
Synchronization Intermezzo • Shared writeable data will become corrupt unless protected • Design aspects: Lock high / lock low • Don’t lock on the first line of Main()
Synchronization Problems • Resources are now protected • Performance is not: Contention • Execution flow is not: • Wait chain: T1 -> M1 -> T2 -> M2 -> T1 • Forward progress: • Timeouts (cf. DB transactions) • Lock leveling
Demo Lock leveling
Advanced Synchronization Problems • Reentrancy • Lock convoys • N readers, one writer
Architecting Concurrency • Partition • Pipeline • Pipeline X Partition = Grid
Announcing Parallel Extensions for .NET 4.0 (Parallel LINQ, Coordination Data Structures, Task Parallel Library)
Partitioning Patterns • Data-Driven Parallelism • CountdownEvent • PLINQ / Parallel.For
Pipeline Patterns • N-1 producers, N-1 consumers • BoundedQueue • BlockingQueue • Condition variable (Wait/Pulse) • Cyclic buffer
DEMO Concurrency patterns in action
Advanced Patterns • Read/write cache • Init-once
Tooling Support • Multi-threaded profilers • Multi-threaded debuggers • Runtime “bug trappers” • Visual Studio 2010 • Grab the CTP now
Visual Studio 2010 Parallel Stacks • With an abundance of tasks, call stack view is not enough!
Summary • Exit threads; enter tasks • Only parallel applications will survive • CPU-bound: TPL, PLINQ, thread pool • I/O-bound: APM • Shared state is a key problem • Patterns and frameworks are emerging • Tooling support
Related Sessions ARC301: "Velocity": Distributed Cache … Yair Shibak 16:00 – 17:10 Tavor Hall
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.