[关闭]
@nrailgun 2015-09-29T15:52:32.000000Z 字数 1118 阅读 1477

Modeling Distributed Systems with spin chan

强力软件


Distributed systems consist of:

Spin simulates distributed systems with processes and chans.

Declaration

A variable of channel type is declared by initializer.

  1. chan c = [ capacity ] of { type_1, type_2, ..., type_n };

If capacity 1, channel is called "buffered", else "rendezvous" (直连,无缓冲).

Send and Receive

Send statement has the form:

  1. c ! expr_1, expr_2, ..., expr_n;

Receive statement has the form:

  1. c ? var_1, var_2, ..., var_n;

Attension: Rendezvous chan ! and chan ? pairs can't be interleaved. But interleavings occur in buffered chan.

Send chan through chan

chan can also be sent through chan, setting up a private connection between server and client.

  1. chan a = [0] of { int };
  2. chan b = [0] of { chan };
  3. b ! a;

Test chan full / empty

Also, you can check channels for full / empty with full, nfull, empty, and nempty. Don't use the negative operator !

Peek chan

Syntax for receiving messages without removing it:

  1. ch ? <v1, v2, ..., vn>

Pattern matching

Receive statement admits values as arguments. Receive statement is executable if and only if matching succeeds.

  1. c ? 'D', 'E', 'A', v;

Keep in mind

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注