Core Async Go Macro Internals - Part I

Timothy Baldridge · 10K views · 2013 · 1:05:07

Open on YouTube

Functional programmingCompilers, interpreters & program analysisProgramming language designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about how core.async go blocks work internally, such as whether they are CPS, how they park, and why they behave like lightweight threads.

Recommended by 4 people across 4 different months, 2013 to 2016.

What it is

Timothy Baldridge, then at Cognitect, walks through how Clojure's core.async go macro is implemented. He reads through the ioc_macros source and explains how the macro turns ordinary code into a state machine, with a few detours along the way.

What people said

Recommends zcaudate · 2013-12 · Stack Overflow · on “Can we describe Clojure's core.async as 'continuation passing style'?”

In a sense, it is CPS, but the implementation is more like code blocks linked by goto everywhere. There are two videos here by Tim Baldridge talking about the implementation of the go macros. Beware: it is two hours in total and he goes into tremendous detail about how the state machine is implemented and how it works. Core Async Go Macro Internals - Part I [this video] Core Async Go Macro Internals - Part 2 [LINK] There is also a prerequisite videos for macros Deep Walking Macros [LINK]

Recommends leonardoborges · 2014-11 · Stack Overflow · on “How to use lightweight thread in Clojure?”

Beware of the terminology: the clojure example using futures doesn't create a lightweight thread but rather a native OS thread. Rust does the same by default but it has a green thread runtime that provides the lightweight semantics. Reference: [LINK] Clojure doesn't support lightweight threads by default but you can create them via the library core.async [LINK] . So the code would look something like this: (require '[clojure.core.async :as async :refer :all]) (doseq [i (take-nth 2 (rest noms))] (go (print (str i " says hello from a lightweight thread!\n")))) The go macro above will create a proper lightweight thread * * The statement wasn't clear as pointed out in the comments so I'll try an

Recommends RJ Acuña · 2015-11 · Stack Overflow · on “State monad vs atom in Clojure”

You won't bee needing it. But monads are used to build more involved abstractions than plain state. Clojure's core.async's go macro is a state machine and internally it's implemented as a monad. You can watch Tim Baldridge's explanation of it: [this video]

Recommends juan.facorro · 2016-06 · Stack Overflow · on “Do core.async go blocks park themselves, or is there a 'scheduler'?”

`core.async`'s *go blocks* park at certain points where parking operations are used. These are the points in which the `core.async` compiler can park these blocks. If you do some blocking IO operation inside a go block, that is not one of the operations provided by core.async, then you will block one of threads in core.async's thread pool. This is what I understood from Timothy Baldrige's videos Macro Internals part I [this video] and part II [LINK] . Both are very interesting.