1 / 13

Concurrency Abstractions in C#

Concurrency Abstractions in C#. Motivation. Concurrency critical factor in behavior/performance affects semantics of all other constructs advantages of language vs. library Compiler analysis/optimization Clarity of syntax asynchronous communication Occurs at various levels

nero-cross
Download Presentation

Concurrency Abstractions in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Concurrency Abstractions in C#

  2. Motivation Concurrency • critical factor in behavior/performance • affects semantics of all other constructs • advantages of language vs. library • Compiler analysis/optimization • Clarity of syntax • asynchronous communication • Occurs at various levels • Requires language support CS 5204 – Fall, 2008

  3. Basic Constructs – Asynchronous Methods Syntax: asyncpostEvent (EventInfo data) { // method body using data } • calls to async method return “immediately” • method body is scheduled for execution in another thread • no return result is possible • similar to sending message/event CS 5204 – Fall, 2008

  4. Basic Constructs - Chords Example: public classBuffer { public stringGet() & public asyncPut (strings) { returns; } } • illustrates a single chord with two methods • chord body is executed only when all methods in the chord have been called • non-async method call implicitly blocked/queued until chord completes • async method calls are queued until matched (caller not blocked) • at most one non-async method per chord • non-deterministic selection of method calls matched by chord • chord body executes in thread of non-async caller (unless all methods in chord are async methods, in which case a new thread is created) CS 5204 – Fall, 2008

  5. . . . [a] [b] [c] [a] [b] [b] [c] Executing Chords invocation queues (one per method) methods bitmaps (one per chord) execute [a,b] chord execute [b,c] chord CS 5204 – Fall, 2008

  6. “Counting” via Methods classToken publicToken (intinitial_tokens) { for (inti=0; i<initial_tokens; i++) Release(); } publicintGrab (intid) & public asyncRelease() { returnid; } } • allows clients to Grab and Release a limited number of tokens • argument on Grab returned to client CS 5204 – Fall, 2008

  7. Recording “state” via Methods publicclassOneCell { publicOneCell() { empty(); } publicvoidPut(objecto) & private asyncempty() { contains( o ); } public objectGet() & private asynccontains (objecto) { empty(); returnso; } } • methods empty and contains are declared private • methods empty and contains “carries” the state of the cell CS 5204 – Fall, 2008

  8. Reader-Writer Example classReaderWriter { ReaderWriter() { idle(); } public voidShared() & asyncidle() { s(1); } public void Shared() & asyncs(intn) { s(n+1); } public voidReleaseShared() & asyncs(intn) { if (n == 1) idle(); elses(n-1); } public voidExclusive() & asyncidle() {} public voidReleaseExclusive() { idle(); } } CS 5204 – Fall, 2008

  9. Active Object (Actor): Base Class public abstract classActiveObject { protected booldone; abstract protected voidProcessMessage(); publicActiveObject() { done = false; mainLoop(); } asyncmainLoop() { while( !done) { ProcessMessage(); } } • actor: thread per object; repeatedly processes received messages • note: thread created by call to async mainLoop() • abstract class creates basic actor pattern CS 5204 – Fall, 2008

  10. Active Object (Actor): Event Example public classStockServer : ActiveObject { privateArrayListclients = newArrayList(); public async AddClient (Client c) & override protected voidProcessMessage() { clients.Add(c); } public asyncWireQuote (Quoteq) & override protected voidProcessMessage() { foreach (Clientcinclients) { c.UpdateQuote(q); }} public asyncCloseDown() & override protect voidProcessMessage() { done = true; } } • message reception/processing driven by ProcessMessage invocations in mainLoop CS 5204 – Fall, 2008

  11. Implementation Outline CS 5204 – Fall, 2008

  12. Performance CS 5204 – Fall, 2008

  13. Syntactic Extension classReaderWriter { asyncidle(); asyncs(int); ReaderWriter() { idle(); } public voidShared() whenidle() { s(1); } whens(intn) { s(n+1); } public voidReleaseShared() whens(intn) { ifn==1) idle(); elses(n-1); } public void Exlcusive() whenidle() {} public voidReleaseExclusive() { idle(); } } CS 5204 – Fall, 2008

More Related