Goare与Hoare的CSP语言的主要区别

Goare与Hoare的CSP语言的主要区别

问题描述:

Look at this statement taken from The examples from Tony Hoare's seminal 1978 paper:

Go's design was strongly influenced by Hoare's paper. Although Go differs significantly from the example language used in the paper, the examples still translate rather easily. The biggest difference apart from syntax is that Go models the conduits of concurrent communication explicitly as channels, while the processes of Hoare's language send messages directly to each other, similar to Erlang. Hoare hints at this possibility in section 7.3, but with the limitation that "each port is connected to exactly one other port in another process", in which case it would be a mostly syntactic difference.

I'm confused.

Processes in Hoare's language communicate directly to each other. Go routines communicate also directly to each other but using channels.

So what impact has the limitation in golang. What is the real difference?

请看以下摘自 Tony Hoare 1978年的开创性论文中的示例: p>

Go的设计受到Hoare论文的强烈影响。 尽管Go与本文中使用的示例语言明显不同,但是示例仍然很容易翻译。 除了语法外,最大的区别是Go明确地将并发通信的管道建模为通道,而Hoare的语言处理过程则像Erlang一样直接彼此之间发送消息。 Hoare在7.3节中暗示了这种可能性,但是存在局限性,即“每个端口在另一进程中正好连接到另一个端口”,在这种情况下,这在语法上将是一个很大的差异。 p> blockquote>

我很困惑。 p>

Hoare语言中的进程直接相互通信。 Go例程也可以直接相互通信,但要使用通道。 p>

那么,什么影响对golan*生了限制。 真正的区别是什么? p> div>

That's exactly the point: in the example language used in Hoare's initial paper (and also in Erlang), process A talks directly to process B, while in Go, goroutine A talks to channel C and goroutine B listens to channel C. I.e. in Go the channels are explicit while in Hoare's language and Erlang, they are implicit.

See this article for more info.

The answer requires a fuller understanding of Hoare's work on CSP. The progression of his work can be summarised in three stages:

  • based on Dijkstra's semaphore's, Hoare developed monitors. These are as used in Java, except Java's implementation contains a mistake (see Welch's article Wot No Chickens). It's unfortunate that Java ignored Hoare's later work.

  • CSP grew out of this. Initially, CSP required direct exchange from process A to process B. This rendezvous approach is used by Ada and Erlang.

  • CSP was completed by 1985, when his Book was first published. This final version of CSP includes channels as used in Go. Along with Hoare's team at Oxford, David May concurrently developed Occam, a language deliberately intended to blend CSP into a practical programming language. CSP and Occam influenced each other (for example in The Laws of Occam Programming). For years, Occam was only available on the Transputer processor, which had its architecture tailored to suit CSP. More recently, Occam has developed to target other processors and has also absorbed Pi calculus, along with other general synchronisation primitives.

So, to answer the original question, it is probably helpful to compare Go with both CSP and Occam.

  1. Channels: CSP, Go and Occam all have the same semantics for channels. In addition, Go makes it easy to add buffering into channels (Occam does not).

  2. Choices: CSP defines both the internal and external choice. However, both Go and Occam have a single kind of selection: select in Go and ALT in Occam. The fact that there are two kinds of CSP choice proved to be less important in practical languages.

  3. Occam's ALT allows condition guards, but Go's select does not (there is a workaround: channel aliases can be set to nil to imitate the same behaviour).

  4. Mobility: Go allows channel ends to be sent (along with other data) via channels. This creates a dynamically-changing topology and goes beyond what is possible in CSP, but Milner's Pi calculus was developed (out of his CCS) to describe such networks.

  5. Processes: A goroutine is a forked process; it terminates when it wants to and it doesn't have a parent. This is less like CSP / Occam, in which processes are compositional.

An example will help here: firstly Occam (n.b. indentation matters)

SEQ
  PAR
    processA()
    processB()
  processC()

and secondly Go

go processA()
go processB()
processC()

In the Occam case, processC doesn't start until both processA and processB have terminated. In Go, processA and processB fork very quickly, then processC runs straightaway.

  1. Shared data: CSP is not really concerned with data directly. But it is interesting to note there is an important difference between Go and Occam concerning shared data. When multiple goroutines share a common set of data variables, race conditions are possible; Go's excellent race detector helps to eliminate problems. But Occam takes a different stance: shared mutable data is prevented at compilation time.

  2. Aliases: related to the above, Go allows many pointers to refer to each data item. Such aliases are disallowed in Occam, so reducing the effort needed to detect race conditions.

The latter two points are less about Hoare's CSP and more about May's Occam. But they are relevant because they directly concern safe concurrent coding.