Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronicity

 Asynchronicity

An overview of the Task Parallel Library
Demo Code At http://goforth.codeplex.com/SourceControl/latest

RCGoforth

July 13, 2013
Tweet

More Decks by RCGoforth

Other Decks in Programming

Transcript

  1. Topics Why Does Parallel Matter? What is a Task? Continuation

    Async and Await Error Handling Cancellation Task Completion Source Parallel Loops and PLINQ Concurrent Data Access Scheduler and Context Common Pitfalls Concurrency Visualizer
  2. Why Does Parallel Matter? • Hanging application syndrome • Cancellation

    (event handling) • Progress Indication Responsiveness • Servers have crazy numbers of cores • Almost every desktop/laptop has multiple 4 or 8 cores • Even phones are dual and quad core now Performance
  3. Parallel is Hard Breaking out Parallelizable Pieces Thread Management and

    Scheduling Data Access Concurrency Problems Marshalling to a UI Thread Waiting/Signaling
  4. Parallel The Way it Should Be (The TPL) Tasks instead

    of Threads Processor Agnostic Code Simple(r) Synchronization Straightforward, Readable Code
  5. What is a Task? Not a Thread Promise to do

    Work Maintains Context Awaitable Task<T> - Promise with a Result
  6. Create a Task New Task() • Doesn’t start the action

    • Uncommon Task.Factory.StartNew() • Most Versatile • Scheduler and Creation Options • No Asynchronous Delegate (use Unwrap) Task.Run() • Most Common • Cancellation • Asynchronous Delegate Overload
  7. Keeping a Good Thing Going (ContinueWith) Inputs ◦ Cancellation ◦

    Scheduling ◦ Continuation Options Task passed to the continuation ◦ firstTask.ContinueWith((task) => DoMoreStuff(task.result)); Whenever this task completes (Success, Cancel, or Error) do this other action (or function)
  8. Keeping a Good Thing Going Scheduling Options • Hide Scheduler

    • Long Running • Prefer Fairness • Lazy Cancellation • Execute Synchronously Hierarchy Options • Attached To Parent • Deny Child Attach Condition Options • Not/Only on Ran to Completion • Not/Only on Cancelled • Not/Only on Faulted
  9. Async and Await Built in- continuation Returning a task Awaiting

    a task The bit that makes all this worthwhile
  10. Behind the Syntax (A loose approximation) var x = await

    DoTheWorkAysnc() Var awaiter = DoTheWorkAsync().GetAwaiter() SaveState() While(Wait){ If (awaiter.IsCompleted) RestoreState() x = awaiter.result //SetException() SetCancelled() }
  11. Task Completion Sources Create a task from another asynchronous source

    or event. var tcs = new TaskCompletionSource() if (e.Cancelled) tcs.TrySetCanceled(); else if (e.Error != null) tcs.TrySetException(e.Error); else //GetData tcs.TrySetResult(data)
  12. Real code has exceptions If an exception happens on an

    unobserved task, Did it really get thrown? It will bite you later, when the task is finalized.
  13. By default, child tasks are created as detached. Exceptions thrown

    from detached tasks must be handled or rethrown in the immediate parent task; they are not propagated back to the calling thread in the same way as attached child tasks propagated back. The topmost parent can manually rethrow an exception from a detached child to cause it to be wrapped in an AggregateException and propagated back to the joining thread. (stolen from MSDN documentation)
  14. Real code has exceptions AggregateException – Something went wrong, here

    is the list. try { task1.Wait(); //or task1.result } catch (AggregateException ae) { foreach (var e in ae.Flatten().InnerExceptions) { if (e is MyCustomException) { //Handle } } }
  15. Real code has exceptions Remember ContinueWith(OnlyOnFaulted)? Combined with an Extension

    Method… We get a simple, re-usable Exception Handling for our tasks.
  16. Its just that we are going in a different direction

    Cancellation Token ◦ Give your tasks a pager (the token) ◦ The same token source is a number for that pager ◦ Token.Cancel signals the task to return (doesn’t actually bring the task back) ◦ Tasks can get more than one pager with a LinkedToken ◦ Call cancel on a WhenAny to cancel the unfinished tasks Cancellation Timeout ◦ Automatically call if tasks with this pager have not completed yet ◦ When creating the token or later on
  17. Did you see the signs? Tasks must check their pager

    ◦ Check when you get started (In case you were waiting for resources for a while) ◦ The check has a cost, but should be checked regularly for long running task Dealing with cancellation in the task ◦ ThrowIfCancellationRequested() ◦ OperationCanceledException ◦ CancellationToken.IsCancellationRequested ◦ Something else based on the context or domain
  18. Progress Reporting Requires designing progress reporting async tasks Extend IProgress

    and pass it to the task Use Progress.Report in your task to send information to the Progress Object
  19. Parallel Simplified for simple operations • Doesn’t guarantee parallel execution,

    only permits it. • Run each iteration as a different task Parallel.For/ForEach • Use the default or build one that suits the data and task type Partitioner • Cancellation Token • Max Degree of Parallelism • Task Scheduler Options
  20. PLINQ .AsParallel() Act on data sets in parallel, data sets

    must be thread safe for the actions in the query Some query constructions will always fall back to sequential execution, more in .NET 4 than in 4.5. In 4.5 only ElementAt() and “Positional” extensions will always be sequential
  21. System.Collections.Concurrent Type Description BlockingCollection<T> Provides bounding and blocking functionality for

    any type that implements IProducerConsumerCollection<T>. ConcurrentDictionary<TKey, TValue> Thread-safe implementation of a dictionary of key-value pairs. ConcurrentQueue<T> Thread-safe implementation of a FIFO (first-in, first-out) queue. ConcurrentStack<T> Thread-safe implementation of a LIFO (last-in, first-out) stack. ConcurrentBag<T> Thread-safe implementation of an unordered collection of elements. IProducerConsumerCollection<T> The interface that a type must implement to be used in a BlockingCollection.
  22. System.Threading Light(er) Weight Locks Type Description Barrier Enables multiple threads

    to work on an algorithm in parallel by providing a point at which each task can signal its arrival and then block until some or all tasks have arrived CountdownEvent Simplifies fork and join scenarios by providing an easy join mechanism ManualResetEventSlim Like ManualResetEvent, but can only be used within a process SemaphoreSlim Like Semaphore, but doesn’t wrap the Win32 Semaphore SpinLock A mutual exclusion lock primitive that causes the thread that is trying to acquire the lock to spin for a period of time before yielding its quantum. SpinLock offers better performance than other forms of locking if the wait time is short SpinWait A small, lightweight type that will spin for a specified time and eventually put the thread into a wait state if the spin count is exceeded
  23. Scheduling and Context The .NET Framework 4 ThreadPool features a

    work-stealing algorithm to help make sure that no threads are sitting idle while others still have work in their queues. When a thread-pool thread is ready for more work, it first looks at the head of its local queue, then in the global queue, and then in the local queues of other threads WPF Synchronization Context Demo
  24. Common Pitfalls Async void – (fire and forget) ◦ Returns

    “less than void” ◦ Use only for top level event handlers The hidden exception – ◦ Either handle sub tasks exceptions in the current task ◦ Or attach the task as a child task Impatient Code – ◦ Ensure that you aren’t just waiting on starting a task ◦ ConfigureAwait(false);