Re:Go Concurrency Patterns ported to Clojure
by bkirkbri on Monday September 16, 2013 @01:39PM
(#6394519)
I've enjoyed working with core.async once I understood these constraints. It would be nice for code to "know" whether it's within a go block or not. That would alleviate needing do-something! and do-something!! versions of some functions.
By "know" I mean something like an in-go earmuff var.
EDITED for clarification and formatting
Re:Go Concurrency Patterns ported to Clojure
by masklinn on Tuesday September 17, 2013 @03:35AM
(#6398089)
> It would be nice for code to "know" whether it's within a go block or not.
Part of it obviously does since `<!` can't be called outside of a go block. And cljs has somewhat less of an issue there as it does not have `<!!` (since without extension the language has very little allowances for blocking)
Re:Go Concurrency Patterns ported to Clojure
by jbert on Monday September 16, 2013 @12:15PM
(#6393979)
Thanks. I misunderstood your (original, unedited) comment to refer to a golang goro (go func()), not clojure go macro.
Yes - this seems to be a key difference of golang's approach. The runtime+scheduler conspire to make a blocking goro much cheaper than an OS thread.
Code inside (thread) blocks run on a dedicated thread. This allows for long running or IO bound processes to execute without clogging the main thread pool. Thread blocks do not rewrite their bodies into a state machine (unlike go blocks) and as such must use <!! and >!!. Since these threads are dedicated they do not return the thread to the thread pool when blocking. This allows for slightly better performance at the cost of an entire JVM thread for the life of the block.
TL;DR - Use <! and >! with go. Use <!! and >!! with thread. Mixing the two can have very unexpected results. In fact, using <! inside a thread block will result in a runtime exception.