3 ms·
Pretty cool use case for virtual threads. Recently I implemented something with core.async in order to batch messages and dispatch them to some UI code at a fix
by koito17 2y ago
Pretty cool use case for virtual threads. Recently I implemented something with core.async in order to batch messages and dispatch them to some UI code at a fixed rate. It was definitely a lot more complicated than using a single sempahore like shown in this article.
On a related note, it would be nice to have an implementation of CSP using proper virtual threads rather than the thread pool currently used by core.async. The promesa library currently has a proof-of-concept[1], but it doesn't seem interoperable with existing core.async code.
[1] https://funcool.github.io/promesa/latest/promesa.exec.csp.html https://funcool.github.io/promesa/latest/promesa.exec.csp.ht...
- andersmurphy 2y ago(author here) Virtual threads being "free" definitely makes things simpler to implement, I was actually quite surprised. Where I think things would get more complicated is if you wanted to control how bursty the token bucket is. You'd probably need another semaphore to limit the bursts etc. This implementation is naive and does create a virtual thread for each permit that is waiting, so you have 2x the number of threads as you would have with a more complex implementation (i.e 1 per running task and 1 per waiting permit). This would be a no go with regular threads, but seems to work fine with virtual threads. It's pretty cool that virtual threads make naive implementations work.