concurrency in c l.
Skip this Video
Loading SlideShow in 5 Seconds..
Concurrency in C ? PowerPoint Presentation
Download Presentation
Concurrency in C ?

Loading in 2 Seconds...

play fullscreen
1 / 70

Concurrency in C ? - PowerPoint PPT Presentation

  • Uploaded on

Concurrency in C . Nick Benton. Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook C é dric Fournet Georges Gonthier Andy Gordon. Tony Hoare Andrew Kennedy Simon Marlow Simon Peyton Jones Claudio Russo Don Syme + visitors. PPT: People. Exciting Times….

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Concurrency in C ?' - elina

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
ppt people
Luca Cardelli

Nick Benton

Karthik Bhargavan

Gavin Bierman

Byron Cook

Cédric Fournet

Georges Gonthier

Andy Gordon

Tony Hoare

Andrew Kennedy

Simon Marlow

Simon Peyton Jones

Claudio Russo

Don Syme

+ visitors

PPT: People
exciting times
Exciting Times…
  • C# and Java are strongly typed, garbage collected languages with language-level security mechanisms
    • this was the preserve of academic languages ten years ago!
  • Parametric polymorphism in C# 2.0 and Java 5
  • Mixed-language programming is really happening
  • Concurrency is going mainstream
  • Using advanced analysis tools for finding bugs and checking correctness properties is becoming routine
  • Contracts and behavioural types are hot topics
    • Only go back ten years in academia
  • Security matters
ppt some other projects
PPT: Some Other Projects
  • Generics for C# and Microsoft® .NET
  • SML.NET, Haskell, F#
  • Stack-walking Security in .NET
  • Formal Tools for Securing Web Services
  • Query Languages for Semi-structured Data
  • First-class Functions for Microsoft® Excel
  • Languages for Systems Biology
  • Software Transactional Memory
why c w
Why Cw?
  • Target domain: distributed Web applications
  • Common “three tier” architecture:
      • Data services tier (database)
      • Middle tier (main application) – C#/Java
      • User interface tier (XML/HTML)
  • Concurrency scenario: asynchronous events and message passing
      • Web services, workflow orchestrations, …
the reality
The Reality
  • It’s about both
    • DATA
      • Objects, XML, relations
      • Asynchrony, concurrency
  • Even though our languages are “modern”, support for these features is pretty primitive
  • Data access is weakly typed, mostly string based
  • Control uses concepts from the ’60s (Monitors, etc.)
  • Essence: these important features are left to APIs 
the guiding principle
The Guiding Principle
  • Provide better level of abstraction
  • Make invariants and intentions more apparent (part of the interface)
  • Give stronger compile-time guarantees (types)
  • Enable different implementations and optimizations
  • Expose structure for other tools to exploit (example: static analysis)

Put (really) important features in the

language itself, rather than in libraries

the c project in a slide
The CProject in a Slide
  • We’re building an experimental language with
    • C# at its core
    • First-class support for common data models
    • A better model of concurrency for both local and distributed concurrency
  • Second principle: RISCy language extension
    • Don’t bake complex solutions into language
    • Choose minimal pieces from which solutions can be built
    • Those pieces having already been shown effective in `purer’ more academic settings
    • Impedance match to new setting (and even user profile)
cw data very briefly
Cw Data (very briefly)
  • New types:
    • Streams (T*)
    • Choice (choice{int;string;})
    • Tuples (struct{int i;string s;})
    • Content classes
    • Nullables (T?)
  • Minimal extensions to allow (fairly) high fidelity, strongly typed, mapping of both relational and semistructured data
  • XML literals (OK, so that’s not very RISCy)
  • Generalized member access (cf. XPath)
  • SQL-style selectqueries
asynchrony is where it s at
Asynchrony Is Where It’s At
  • Distribution → concurrency + latency → asynchrony→ more concurrency
  • Message-passing, event-based programming, dataflow models
  • For programming languages, coordination (orchestration) languages and frameworks, workflow
net today
.NET Today
  • Java-style “monitors”
  • Operating system shared memory primitives
  • Clunky delegate-based asynchronous calling model
  • Hard to understand, use and get right
    • Different models at different scales
    • Support for asynchrony all on the caller side – little help building code to handle messages (must be thread-safe, reactive, and deadlock-free)
c concurrency
  • New concurrency model and language constructs
  • Based on the join calculus
    • A foundational process calculus like the p-calculus but better suited to asynchronous, distributed systems
  • A single model which works both for
    • local concurrency (multiple threads on a single machine)
    • distributed concurrency (asynchronous messaging over LAN or WAN)
  • It is different
  • But it’s also simple
c concurrency in one slide
C Concurrency in One Slide
  • Objects have both synchronous and asynchronous methods
  • Values are passed by ordinary method calls:
    • If the method is synchronous, the caller blocksuntil the method returns some result (as usual)
    • If the method is async, the call completes at once and returns void
  • A class defines a collection of chords (synchronization patterns), which define what happens once a particular set of methods have been invoked. One method may appear in several chords.
    • When pending method calls match a pattern, its body runs.
    • If there is no match, the invocations are queued up.
    • If there are several matches, an unspecified pattern is selected.
    • If a pattern containing only async methods fires, the body runs in a new thread.
a simple buffer for use by producer consumer threads
A Simple Buffer(for use by producer/consumer threads)

class Buffer {

async put(string s);

stringget() & put(string s) {

return s;



a simple buffer
A Simple Buffer

class Buffer {

async put(string s);

stringget() & put(string s) {

return s;



  • An ordinary (synchronous) method with no arguments, returning a string
  • An ordinary (synchronous) method with no arguments, returning a string
a simple buffer17
A Simple Buffer

class Buffer {

async put(string s);

stringget() & put(string s) {

return s;



  • An ordinary (synchronous) method with no arguments, returning a string
  • An asynchronous method (hence returning no result), with a string argument
a simple buffer18
A Simple Buffer

class Buffer {

async put(string s);

stringget() & put(string s) {

return s;



  • An ordinary (synchronous) method with no arguments, returning a string
  • An asynchronous method (hence returning no result), with a string argument
  • Joined together in a chord
a simple buffer19
A Simple Buffer

class Buffer {

async put(string s);

stringget() & put(string s) {

return s;



  • Calls toput()return immediately, but are internally queued if there’s no waitingget()
  • Calls toget()block until/unless there’s a matchingput()
  • When there’s a match the body runs, returning the argument of theput()to the caller ofget()
  • Exactly which pairs of calls are matched up is unspecified
a simple buffer20
A Simple Buffer

class Buffer {

async put(string s);

stringget() & put(string s) {

return s;



  • Does this example involve spawning any threads?
    • No. Though the calls will usually come from different pre-existing threads.
  • Is it thread-safe? You don’t seem to have locked anything…
    • Yes. The chord compiles into code which uses locks. (And that doesn’t mean everything is synchronized on the object.)
  • Which method gets the returned result?
    • The synchronous one; there is at most one of these in a chord.
reader writer

…using threads and mutexes in Modula 3

An introduction to programming with threads

Andrew D. Birrell, January 1989

reader writer in five chords
Reader/Writer in Five Chords

publicclass ReaderWriter {

privateasync idle();

privateasync s(int n);

public ReaderWriter() {idle();}

publicvoid Exclusive() & idle() {}

publicvoid ReleaseExclusive() { idle(); }

publicvoid Shared() & idle() { s(1);}

& s(int n) { s(n+1);}

publicvoid ReleaseShared() & s(int n) {

if (n == 1) idle(); else s(n-1);



A single private message represents the state:

none↔idle()↔s(1) ↔s(2) ↔ s(3) …

asynch requests responses
Asynch Requests/Responses
  • Service exposes an async method which takes parameters and somewhere to put the result:
    • a buffer, or a channel, or
    • here, an asynchronous delegate, used as a callback

publicdelegateasync IntCB(int v); // the callback

publicclass Service {

publicasync request(String arg, IntCB callback){

… // compute result

callback(result); // respond!

} }

joining responses
Joining Responses

class Join2 {

async first(int r);

async second(int r);

struct{int;int;} waitAll() &

first(int r1) &

second(int r2) {




// client code:

struct{int;int;} results;

Join2 x = new Join2();

service1.request(arg1, new IntCB(x.first));

service2.request(arg2, new IntCB(x.second));

// do something useful in the meantime

// now wait until both results have come back

results = x.waitAll();

// do something with results

selecting responses
Selecting Responses

class Select2 {

async reply(int r);

int waitOne() & reply(int r) {

return r;



// client code:

int result;

Select2 x = new Select2();

service1.request(arg1, new IntCB(x.reply));

service2.request(arg2, new IntCB(x.reply));

// do something useful in the meantime

// now wait until one result has come back

result = x.waitOne();

// do something with result

active objects
Active Objects

publicabstractclass ActiveObject : MarshalByRefObject {

protectedbool done;

abstractprotectedvoid processmessage();

public ActiveObject () {

done = false; mainloop(); }

async mainloop() {

while (!done) { processmessage(); }




class Stock : ActiveObject {

publicasync bid(BidOffer thebid);

publicasync register(Client who);

overrideprotectedvoid processmessage()

& bid(BidOffer thebid) {

// process bid messages


& register(Client who) {

// process registration requests



extending c with chords
Extending C# with chords
  • Syntax extensions:
    • Declarations for asynchronous methods
    • Definitions of (synchronous) methods generalized to chord definitions
    • when for purely asynchronous chords
  • Interesting well-formedness conditions:
    • Can’t have two synchronous methods in a single pattern
    • The inheritance restriction
    • “ref” and “out” parameters cannot appear in async headers
why 1 synchronous method in a chord
Why ≤1 synchronous method in a chord?
  • JoCaml allows multiple synchronous methods to be joined, as in the following rendezvous
  • But in which thread does the body run? In C#, thread identity is “very” observable, since threads are the holders of particular re-entrant locks. So we rule this out in the interests of keeping & commutative. (Of course, it’s still easy to code up an asymmetric rendezvous in Polyphonic C#.)

int f(int x) & int g(int y) {

return y to f;

return x to g;


the problem with inheritance
The problem with inheritance

class C {

virtualasync g();

virtualasync h();

virtualvoid f() & g() {…}

& h() {…}


class D : C {

overrideasync g();


  • We’ve “half” overridden f
  • Too easy to create deadlock or async leakage

void m(C x) { x.g(); x.f();}

m(new D());

the inheritance restriction
The inheritance restriction
  • Two methods are co-declared if there’s a chord with both of them in

Whenever a method is overridden,every co-declared method must also be overridden

  • privateasync g()
  • publicvirtualvoid f() & g() {…}
  • Inheritance and concurrency don’t mix well.Our restriction is simple; it could be made less restrictive.
types etc
Types etc.
  • async is a subtype of void
  • Allow covariant return types on those two:
    • An async method may override a void one
    • A void delegate may be created from an async method
    • An async method may implement a void method in an interface
  • async methods are given the [OneWay] attribute, so remote calls are non-blocking
  • 4th version…
  • Built on CCI (also used for Spec#), runs in VS
  • Introduce queues for pending calls (holding blocked threads for sync methods, arguments for asyncs)
  • Generated code (using brief lock to protect queue state) looks for matches and then either
    • Enqueues args (async no match)
    • Enqueues thread and blocks (sync no match)
    • Dequeues other args and continues (sync match)
    • Wakes up blocked thread (async match with sync)
    • Spawns new thread (async match all async)
  • Efficient – bitmasks to look for matches, no PulseAlls,…
  • We’ve changed the underlying implementation to match changes in CLR performance (sleeping/interrupting -> waiting/pulsing)
predictable demo dining philosophers
Predictable Demo: Dining Philosophers


to eat



to eat



code extract
Code Extract

class Room {

privateasync hasspaces(int n);

privateasync isfull();

public Room (int size) { hasspaces(size); }

publicvoid enter() & hasspaces(int n) {

if (n > 1) hasspaces(n-1);

else isfull();


publicvoid leave()

& hasspaces(int n) {



& isfull() {






Dining philosophers

other samples
Other Samples
  • Web service combinators (Cardelli & Davies)
  • Adaptive scheduler (cf. Larus & Parkes),
  • Accessing web services (Terraserver),
  • Active objects and remoting (stock trader)
  • Santa Claus problem (Trono)
  • 3 internal projects use joins of some form
  • External
    • MC#
    • Nemerle
    • QUT web service language
    • Spry
current and future work
Current and future work
  • Extension with generics
  • Limited pattern-matching on message contents
    • Been prototyped (Melgratti) but very hard to compile efficiently
    • Direct syntactic support for timeouts along this line would be nice…
  • Adding joinable transactions with explicit compensations (Bruni, Montanari)
  • Behavioural types
  • Integration/compilation with STM???
  • A clean, simple, new model for asynchronous concurrency in C#
    • Declarative, local synchronization
    • Model good for both local and distributed settings
    • Efficiently compiled to queues and automata
    • Easier to express and enforce concurrency invariants
    • Compatible with existing constructs, though they constrain our design somewhat
    • Minimalist design – pieces to build whatever complex synchronization behaviours you need
    • Solid foundations
    • Works well in practice
compiler release
Compiler Release
  • Freely available (v.1.0.2) from
  • Over 7000 downloads so far 
  • Please download and play!
for more information
For More Information
    • Papers, compiler, links
  • ShowPost.aspx?PostID=23947
    • Channel 9 video
    • A public discussion forum
    • Not run by Microsoft/Microsoft Research

Nick Benton

Gavin Bierman

Luca Cardelli

Cédric Fournet

Erik Meijer

Claudio Russo

Wolfram Schulte

Herman Venter

Mark Shinwell, Hernan Melgratti, and all the other folks who’ve contributed to the implementation

one shot timeoutbuffer
One-shot TimeoutBuffer

class TimeoutBuffer {

async empty();

async has(Object o);

async timeout();

TimeoutBuffer(int delay) {

Timer t = new Timer(new TimerCallBack(this.tick),delay);



void put(Object o) & empty() {has(o);}

& timeout() {timeout();}

void tick() & empty() {timeout();}

& has(Object o) {has(o);}

Object get() & timeout() {timeout(); throw new TimeOutExn();}

& has(Object o) {has(o); return o;}


fairer reader writer lock
Fairer reader/writer lock

class ReaderWriterFair


ReaderWriter() { idle(); }

private int n = 0; // protected by s() or t()

public void Shared() & async idle() { n=1; s(); }

public void Shared() & async s() { n++; s(); }

public void ReleaseShared() & async s() {

if (--n == 0) idle(); else s();


public void Exclusive() & async idle() {}

public void ReleaseExclusive() { idle(); }

public void ReleaseShared() & async t() {

if (--n == 0) idleExclusive(); else t();


public void Exclusive() & async s() {

t(); wait();


void wait() & async idleExclusive() {}


current data access in c

Queries stored

as strings



Using string to

project attributes

Runtime type


Current Data Access in C#

SqlConnection conn = new SqlConnection(…);


Console.WriteLine("Please enter a city: ");

string input = Console.ReadLine();

SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE



SqlDataReader results = cmd.ExecuteReader();

Console.WriteLine("Customers from: "+input);

while (results.Read())


string Name = (string)results["ContactName"];

string Phone = (string)results["Phone"];

Console.WriteLine(Name + " -- "+Phone);



api access continued
API Access (Continued)
  • Storing queries as strings is not only ugly but also a security risk
    • Imagine where city=“’ OR 1=1 --”
      • This will return the entire relation!!
      • This is the source of many security loopholes in Web-based databases
use parameters and typed datasets
Use Parameters and Typed Datasets…

SqlDataAdapter da = new SqlDataAdapter(

"SELECT * FROM Customers WHERE City= @city",

conn );

SqlParameter cityParam =

da.SelectCommand.Parameters.Add("@city", SqlDbType.VarChar, 80);

cityParam.Value = input;

NorthwindDataSetds = new NorthwindDataSet();


foreach (NorthwindDataSet.CustomersRow dr inds.Customers.Rows)


string Name =dr.ContactName;

string Phone =dr.Phone;

Console.WriteLine( Name + " -- " + Phone);


or use stored procedures
Or Use Stored Procedures…

CREATE PROCEDURE CustomersForCity @City nvarchar(80)

AS SELECT * FROM Customers WHERE City = @City

SqlCommand cmd = new SqlCommand("dbo.CustomersForCity",conn );

cmd.CommandType = CommandType.StoredProcedure;

SqlParameter cityParam = cmd.Parameters.Add("@City",

SqlDbType.VarChar, 80);

cityParam.Value = input;

SqlDataAdapter da = new SqlDataAdapter( cmd );

NorthwindDataSet ds = new NorthwindDataSet();

da.Fill(ds, ds.Customers.TableName );

foreach (NorthwindDataSet.CustomersRow dr in ds.Customers.Rows)


string Name =dr.ContactName;

string Phone =dr.Phone;

Console.WriteLine( Name + " -- " + Phone);


current xml access in c
Current XML Access in C#

staticvoid UseXPathDocument(string reviewsLocation, string bibLocation, XmlTextWriter xtw) {

XPathDocument bibDoc = newXPathDocument(bibLocation);

XPathDocument reviewsDoc = newXPathDocument(reviewsLocation);


XPathNodeIterator books = bibDoc.CreateNavigator().Select("/bib/book");

while (books.MoveNext()) {

XPathNodeIterator entries = reviewsDoc.CreateNavigator().Select("/reviews/entry");

XPathNodeIterator bookTitleItr = books.Current.SelectChildren("title","");


XPathNodeIterator bookPriceItr = books.Current.SelectChildren("price","");


while(entries.MoveNext()) {

XPathNodeIterator entryTitleItr = entries.Current.SelectChildren("title","");


XPathNodeIterator entryPriceItr = entries.Current.SelectChildren("price","");


if (entryTitleItr.Current.Value == bookTitleItr.Current.Value) {




    }  }



minimal extensions to c
Minimal Extensions to C#
  • New types:
    • Streams (T*)
    • Choice (choice{int;string;})
    • Tuples (struct{int i;string s;})
    • Content classes
    • Nullables (T?)
    • async(subtype of void)
  • XML literals
  • Chords
  • Generalized member access (cf. XPath)
  • SQL-style selectqueries
  • New type:T*(sequence ofT)
  • Close relative of IEnumerable<T>
  • Streams generated by statement blocks that yield values
  • Streams consumed by foreach loop
  • No nested streams (no T**)
    • Just like in XQuery/XPath
streams example
Streams Example

publicstaticint* FromTo(int s, int e) {

for (i = s; i <= e; i++) yieldreturn i;


int* OneToTen = FromTo(1,10);

foreach(int j in OneToTen) {



why streams
Why Streams?
  • We use streams to represent
    • The (homogenous) sequence of rows from a database table
    • The iteration operator from XML Schema/DTDs

<!ELEMENT foo (bar*)>

anonymous structs
Anonymous structs
  • New type:
  • Constructed in obvious way:
  • Project using dot notation:
  • Like ML records but ordered, duplicates allowed

struct{int i; string s; string s;}

struct{int i; string s; string s;} tup =

new{i=42, s=“Gavin”, s=“Nick”};

int temp = tup.i;

why anonymous structs
Why Anonymous structs?
  • We use anonymous structs to represent
    • A row (of heterogeneous fields) from a SQL table
    • Ordered sequences of XML Schema/DTD elements, example:

<!ELEMENT foo (bar,pop,pop)>

union types
Union Types
  • New type: choice{T;S;}
  • A value of this type is either a value of type T or a value of type S, example:
  • Why?
    • To represent choice in XML Schema/DTD

<!ELEMENT foo (bar | pop)>

choice{string; Button; int;} u = “hello”;

u = new Button();

u = 42;

content class
Content Class
  • Like a normal class, except it contains a single unnamed member
  • Useful for XML fidelity

publicclass book {


string title;

choice{struct{editor editor;}*;

struct{author author;}*;


string publisher;

decimal price;



xml literals
XML Literals
  • Now we have the types, we can write (strongly typed) XML literals in our code:

<!ELEMENT bib (book*)>

<!ELEMENT book (title,

(author* | editor*), publisher, price)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT publisher (#PCDATA)>

<!ELEMENT price (#PCDATA)>

bib = <bib>


<title>TCP/IP Illustrated</title>





<price> 65.95</price>



<title>The Economics of Technology and

Content for Digital TV</title>





<publisher>Kluwer Academic</publisher>




publicclass bib

{ struct{ book book; }*; }

publicclass book

{ struct {string title;

choice {

struct { editor editor;}*;

struct { author author;}*;}

string publisher;

decimal price;}


generalized member access gma
Generalized Member Access (GMA)
  • A key design feature of C
  • Observe: C can encode XML-like values
  • Want: some query-like facility
  • Solution: extend the dot notation behaviour (to match “/” from XPath)

The power is in the dot!

generalized member access
Generalized Member Access
  • Sequences
  • Anonymous struct types
  • Choice types

string* ss;



struct{int i; string s; string s;} x;





choice{string;Person;int;} y;



xquery use case three
XQuery Use Case Three

For each book in the bibliography, list the title and authors, grouped inside a “result” element







for $b in $bs/book






foreach (b in









path expressions
Path Expressions
  • GMA allows us to write OQL-like path expressions:

staticint* FromTo(int s, int e) {

for (i = s; i <= e; i++) yieldreturn i;





demonstration one

Demonstration One

Data access in C

sql like queries
SQL-Like Queries
  • We support first-class SQL queries

String conn = “ ...Initial Catalog=Northwind; ";

Database DB = new Database(conn);

Console.WriteLine("Please enter a city: ");

string input = Console.ReadLine();

res = select *

from DB.Customers

where City==input ;

Console.WriteLine("Customers from: "+input);

res.{Console.WriteLine(it.ContactName +

" --- " +


other work
Other Work
  • The type system and operational semantics of (the data access fragment of) C have been formally (i.e. mathematically) specified
    • To be presented at ECOOP 2005
  • C has other data access goodies
    • More XPath-like extensions
    • More SQL extensions
  • C extends C# with first class support for data access and asynchronous concurrency
  • Data access extensions
    • Model good for both relational (SQL) and semi-structured (XML) data
    • Compile to either in-memory operations or external queries
    • Based on ideas from functional programming, but tweaked for compatibility with objects and XPath
  • Concurrency model
    • Model good for both local and distributed settings
    • Efficiently compiled to queues and automata
    • Based on join-calculus, but tweaked for compatibility with objects
relationship with c
Relationship with C#
  • Note: C is not C# 3/4/5… !
    • We’re a research project
    • We’re not sponsored by the C# or VS team
    • [Actually, the C# team look at lots of research internal and external]

"The C# team is excited about C and other C#-based research projects that MSR-Cambridge is working on. We have no current plans to extend the C# language in this direction, but will continue to observe the progress of C and other MSR-Cambridge projects."

Scott Wiltamuth, C# Product Unit Manager