The Crystal Programming Language Forum

What is `select` re concurrency?

The blog post announcing parallelism mentions select as a concurrency statement, and the release notes for 0.33.0 do too.

I’ve tried to find documentation about said select in the reference guide or docs without luck (perhaps I missed it?). Fiber#select seems like a candidate, but has no docs.

Can anyone give a crash course about it?

It’s derived from Go, best bet would be checking out their docs for the moment.

https://gobyexample.com/select

I see! Which would be the equivalent syntax for

select {
case msg1 := <-c1:
    fmt.Println("received", msg1)
case msg2 := <-c2:
    fmt.Println("received", msg2)
}

?

Probably something like

select
when msg1 = ch1.receive
  puts "received #{msg1}"
when msg2 = ch2.receive
  puts "received #{msg2}"
end
1 Like

We have some documentation material planned to give better visibility to the select, but for now https://gist.github.com/bcardiff/289953a80eb3a0512a2a2f8c8dfeb1db can help to understand the different options.

3 Likes

Yeah with the changes related to timeout I think we can assume the API is stable enough to write the documentation.