First published at 22:48 UTC on July 18th, 2018.
This tutorial shows how to use the Chemical Machine to implement all the concurrency primitives of the Go language: the "goroutines", the "channels", and the "select" construction.
The main similarity between the Chemi…
MORE
This tutorial shows how to use the Chemical Machine to implement all the concurrency primitives of the Go language: the "goroutines", the "channels", and the "select" construction.
The main similarity between the Chemical Machine paradigm and the CSP paradigm (as implemented in the Go language) are that channel "send" action is somewhat similar to emitting a molecule. When a message is "sent on a channel", it waits until someone "receives" it. This is similar to how an emitted molecule remains at the reaction site until some reaction consumes it.
At that point, similarities end and differences begin:
- Sending and receiving are blocking operations by default, while emitting a molecule is a non-blocking operation in the chemical machine
- Reactions start automatically on the chemical machine, while "goroutines" need to be started manually
- The Chemical Machine encourages data-driven reasoning, while the Go language requires reasoning about processes that run and send messages to each other at a given time
- Contention on which reaction can consume a molecule is defined statically in the chemical machine, while the Go language allows the developer to define new code that contends on reception from previously defined channels
- The chemical machine helps the programmer to expose potentially ambiguous and non-deterministic code, which the Go language does not make apparent
I illustrate these differences by running example code:
- the "select" construction
- the "ping-pong" example from https://talks.golang.org/2013/advconc.slide#6
Code for this presentation: https://github.com/Chymyst/Chymyst/blob/master/src/test/scala/io/chymyst/lab/ChymystGoLang.scala
See also "Concurrency in Reactions", a tutorial book on `Chymyst` and Chemical Machine programming: https://winitzki.gitbooks.io/concurrency-in-reactions-declarative-multicore-in/content/
Main repository for the `Chymyst` library: https://github.com/Chymyst/chymyst-core/
LESS