The Clojure approach to concurrency
Clojure provides four constructs that can help developers to write concurrent programs, as follows:
- Refs
- Agent
- Atoms
- Vars
Each construct serves a different purpose. In order to understand when to use each one, we will need some ways to classify them. We will provide these in the next sections.
Classifying constructs
If we want to classify Clojure concurrency constructs we can use two continua—coordination and synchronization. When used together, they will help us decide which Clojure concurrency construct to use.
Synchronization
Operations on data are classified as synchronous when one thread waits until it has exclusive access to the required data. On the other hand, asynchronous operations can be performed without blocking access to data by one thread.
Coordination
In a coordinated operation, multiple threads cooperate in order to yield the correct results. In an uncoordinated operation, multiple threads execute in separated contexts, and they cannot impact each...