Fork me on GitHub

This question might be obvious for JVM fokes, but not to me who spent the last years in nodejs-land: When writing core.async programs, my main thread does nothing but kick start all the other processes, that run in go blocks and speak via channels. If the main function is done the program exits. My solution to that was to make an infinite loop that runs until (:should-exit? @app-state) flag is false. That works, but it keeps that thread in the "Running" state, and using CPU just to spin the infinite loop. How can you set your main thread in a JVM Clojure program to wait, until some predicate is true and then exit or change state?


However sets :should-exit? in your app-state might instead send an exit signal to a channel from which the main thread is (blockingly) reading




Wow, that is just so much simpler. Thanks @ska! Really is easy to be stuck in old ways of thinking at times. Thank you for freeing one of my cpu cores 😉


@codemartin: you could have your main thread block on a promise , and deliver a value to that promise when you want the thread to proceed


@codemartin: you can do the same in core.async by using the sync take <!!;!!