Clojurians
#core-async
<
2016-03-10
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

codemartin09:03:09

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?

ska09:03:13

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

ska09:03:22

s/However/Whoever/

codemartin09:03:43

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 😉

mccraigmccraig09:03:54

@codemartin: you could have your main thread block on a promise http://clojuredocs.org/clojure.core/promise , and deliver a value to that promise when you want the thread to proceed

mccraigmccraig09:03:49

@codemartin: you can do the same in core.async by using the sync take <!!http://clojure.github.io/core.async/#clojure.core.async/&lt;!!