Concurrent Pascal - Example

Example

The following example shows the declaration of a simple monitor, and its use by two communicating processes.

type buffer = monitor "bounded buffer monitor" var saved: integer; "saved item is an integer" fullq, emptyq: queue; "used by only two processes" full: boolean; "true if an item is saved:" procedure entry put (item: integer); "puts item in buffer" begin if full then delay(fullq); "block if full" saved := item; "save the item" full := true; "mark as full" continue(emptyq) "unblock consumer" end; procedure entry get(var item: integer); "gets item from the buffer" begin if not full then delay(emptyq); "block if empty" item := saved; "get the item" full := false; "mark as not full" continue(fullq) "unblock producer" end; begin "initialize the monitor" full := false end; producer = process(pass: buffer); "producer uses a buffer" var item: integer; begin cycle "execute in a loop forever" "produce an item" pass.put(item) "pass an item to the monitor" end end; consumer = process(pass: buffer); "consumer uses a buffer" var item: integer; begin cycle pass.get(item); "get an item from the monitor" "consume the item" end end; "declare instances of the monitor, producer, and consumer" "give the producer and consumer access to the monitor" var pass: buffer; prod: producer; cons: consumer; begin init pass, "initialize the monitor" prod(pass), "start the producer process" cons(pass) "start the consumer process" end.

Read more about this topic:  Concurrent Pascal

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)