# CSP examples in Julia?

**URL:** <https://discourse.julialang.org/t/csp-examples-in-julia/20011>\
**Category:** Julia at Scale\
**Created:** [January 24, 2019, 1:57am UTC](https://discourse.julialang.org/t/csp-examples-in-julia/20011 "2019-01-24T01:57:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [January 24, 2019, 1:57am UTC](https://discourse.julialang.org/t/csp-examples-in-julia/20011/1 "2019-01-24T01:57:25Z")

</div>

Golang’s concurrency primitives [were originally inspired by](https://blog.golang.org/share-memory-by-communicating) C. A. R. Hoare’s famous 1978 paper _“Communicating sequential processes”_, ([paper](https://www.cs.cmu.edu/~crary/819-f09/Hoare78.pdf), [book](http://www.usingcsp.com/)).

Here’s a blog post where they talk about it:

> **[Share Memory By Communicating - The Go Programming Language](https://go.dev/blog/codelab-share)**
>
> A preview of the new Go codelab, Share Memory by Communicating.

* * *

This git repo re-implements all of the examples in that original paper in Go, so you can directly compare the ideas to Go’s implementation. It’s really nice!

> **[GitHub - thomas11/csp: Tony Hoare's seminal 1978 paper "Communicating...](https://github.com/thomas11/csp)**
>
> Tony Hoare's seminal 1978 paper "Communicating sequential processes" implemented in Go. - GitHub - thomas11/csp: Tony Hoare's seminal 1978 paper "Communicating sequential pro...

And here’s the fancy godocs documentation page:  
[https://godoc.org/github.com/thomas11/csp](https://godoc.org/github.com/thomas11/csp)

* * *

**Does anyone know if there’s a similar such repo in Julia?**

Julia’s concurrency primitives derive from the same inspiration, and I’d love to see a side-by-side comparison of good julia implementations of these examples!

Some of these could, I think, be expressed more easily in Julia than in Go. For example, I was working on [A Tour of Go](https://tour.golang.org/concurrency/8), and as far as I can tell in Go there’s no great way to alternate reading from two `Channel`s, one then the other:

> <https://stackoverflow.com/questions/20593126/reading-from-multiple-channels-simultaneously-in-golang>

I eventually settled on this:

```golang
func Same(t1, t2 *tree.Tree) bool {
	ch1 := make(chan int)
	ch2 := make(chan int)
	go Walk(t1, ch1)
	go Walk(t2, ch2)
	for ok1,ok2 := true, true; ok1 && ok2; {
		v1, _ok1 := <-ch1
		v2, _ok2 := <-ch2
		if v1 != v2 {return false }
		ok1,ok2 = _ok1,_ok2
	}
	return true
}

```

But in Julia, of course, you can just `zip` them!:

```julia
function same(t1::Tree, t2::Tree)
    c1 = Channel(c->walk(t1, c))
    c2 = Channel(c->walk(t2, c))
    for (v1,v2) in zip(c1,c2)
        v1 != v2 && return false
    end
    return true
end

```

Or, even better,

```julia
function same(t1::Tree, t2::Tree)
    c1 = Channel(c->walk(t1, c))
    c2 = Channel(c->walk(t2, c))
    return all(isequal.(c1, c2))
end

```

* * *

If no one knows of an existing CSP examples repo in julia, let’s make one!

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [January 24, 2019, 2:00am UTC](https://discourse.julialang.org/t/csp-examples-in-julia/20011/2 "2019-01-24T02:00:11Z")

</div>

> If no one knows of an existing CSP examples repo in julia, let’s make one!

I’ve started this empty repo where we can collect the examples if no one else has done this yet:

> **[GitHub - NHDaly/CspExamples.jl: C.A.R. Hoare's 1978 paper, "Communicating...](https://github.com/NHDaly/CspExamples.jl)**
>
> C.A.R. Hoare's 1978 paper, "Communicating Sequential Processes", examples in Julia - GitHub - NHDaly/CspExamples.jl: C.A.R. Hoare's 1978 paper, "Communicating Sequential Proc...

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [July 18, 2019, 12:13am UTC](https://discourse.julialang.org/t/csp-examples-in-julia/20011/3 "2019-07-18T00:13:57Z")

</div>

Fun times, I’ve just enabled multithreading in the CSPExamples repo:  
[https://github.com/NHDaly/CspExamples.jl/pull/6](https://github.com/NHDaly/CspExamples.jl/pull/6)

It was as easy as setting the parameter to allow all the Tasks I create to be scheduled on multiple threads, and everything else just worked without modifications! 🙂

I didn’t have to change any of the implementations at all, which is the point that’s always emphasized by proponents of this CSP / coroutines+channels architecture: _The code structure doesn’t need to change at all to scale your parallelism. You just change the number of threads you’re scheduling on, and that’s it!_
