Comparing Crystal’s concurrency with that of Go (Part II)

Meraj Molla
ITNEXT
Published in
3 min readAug 1, 2020

--

In the first part of this article series, I focused on comparing Crystal and Go’s concurrency from Communicating Sequential Processes (CSP) perspective. This second part is to fill in the gap and cover more traditional Shared Memory Multithreading.

src: http://www.embeddedlinux.org.cn/rtconforembsys/5107final/LiB0095.html

I make the assumption that you are somewhat familiar with the concepts of multiple threads, critical region, mutual exclusion, and lock/unlock to ensure mutual exclusion.

Like my previous article, this one will also focus on converting some Go programs to Crystal and try to compare the two.

Shared Memory Multithreading

In Shared Memory Multithreading, multiple threads can access common shared variable(s) and to avoid race condition and ensure mutual exclusion, locks are used.

Go supports this using sync package

  • Mutex — is used to provide support for lock and unlock. A variant RWMutex is also supported
  • WaitGroup — is used to wait for a collection of goroutines to finish

Crystal supports this using Mutex class. However, There is no WaitGroup support as of version 0.35.1. I will provide an example which demonstrate how to use channel to implement something similar to WaitGroup.

--

--