Question about Workers on Kotlin/Native \ MP on wi...
# coroutines
j
Question about Workers on Kotlin/Native \ MP on windows: (I figured the coroutines channel was still the best place to ask the following, even tho I can't do this with coroutines (yet?).) I've checked out the workers example here [ https://github.com/JetBrains/kotlin-native/blob/master/samples/workers/src/workersMain/kotlin/Workers.kt ] and got that to work, but I am now wondering if there is a way ( in Kotlin, without resorting to sharing a buffer via C or something like that ) to have a long running process ( e.g. another windows program launched via popen ) running in a worker which periodically sends data ( e.g. a string of the output of the program ) back to the main thread. From what I understand, workers allow you to do work in a separate "environment", and then "transfer back" the (immutable) data once the worker is done. Is there a canonical way to periodically "send data back" to the "main thread"?
g
Workers are not related to coroutines directly. Essentially what you looking for is possible with multithreaded coroutines and Flow, it's the best high level primitive for such task, but it's still in development
j
can I do it with just workers at this point?
g
You probably can write some abstraction that runs workers periodically to check state or just launch permanently running worker that does what you need and somehow share state using atomic or some similar approach
Worker is just an abstraction to launch some code in background thread, nothing more
j
can you elaborate what you mean with "using atomic"? Apologies, I'm from the land of virtual machines, have not worked with C in a really long time, and never much.
g
It's not something C related, it's K/N primitive <https//kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/ atomic int/index.html|https//kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-atomic-int/index.html>
j
I see. Any hints how to "wait" for a AtomicInt lock on the main thread? with the coroutine mutex? Or a posix function?
g
You cannot wait, but using coroutines you can poll on it without blocking
Atomic is just a way to communicate