In this post, we talk about the concurrency model introduced in CMU15-440 P0 Part A project. Reading and Learning the Recitation of Go Concurrency will help you gain comprehensive understanding of Go concurrency pattern.
The Go Currency Examples
As you can see, Main routine is where the action get executed. Member functions like withdraw, deposit and checkBalance are all just a way to register action to the main routine. After registering the action, Main routine will select one of the action every time, does not care about the order (in random), But will eventually execute all the action.
drawback
In this implementation, each action does not guarantee to be executed in order, which may lead to problem in other use case. For example the K-V store use case. we need to first Put:Key:value, then we will be able to Get:Key.
compare with traditional C implementation
In C we rely on System mechanism such as semaphore, Mutex, Monitor, Conditional Variable, for Concurrency sychronization. But in Golang, we rely on channel and goroutine, which is managed by go runtime. But underlying the implementation of channel, it still leverage System mechanism.
P0 Implementation
Consideration
This is a relatively easy Project, we get to use with channel and goroutine.
- In keyValueServer we store the information of client connection to manage clients. We maintain a channel for registering command to the server.
- The Concurrency here is we have multiple Clients, each might send command to server in unpredictable timing. We should register the command to the server to keep the commands in order.