I'm using the `konan.worker.*` APIs to do some bac...
# kotlin-native
a
I'm using the
konan.worker.*
APIs to do some background processing (in a loop), and it's working well, but now I'd like to add an ability to gracefully cancel/abort (early-exit) from the background loop. What's the correct way to implement this? Is there some flag I can check for in my background loop that is set when
requestTermination
has been called? I understand there are restrictions on sharing state, so for example I get an
IllegalStateException
if I try to pass in a
BooleanVar
into my worker args.
o
IllegalState is triggered by the case, when variable is still referenced by original owner, maybe deepCopy would help. In your case, maybe just sending an explicit termination request with custom message would help.
a
Yeah, my mistake. You are right in this case it wasn't the termination flag I passed that was a problem but rather a string variable I passed. E.g. passing it like this was not working:
Copy code
worker.schedule(TransferMode.CHECKED, { WorkerArgs(stringVar) }, ::workerFunction)
But this does work:
Copy code
worker.schedule(TransferMode.CHECKED, { WorkerArgs("$stringVar") }, ::workerFunction)
(e.g. making a new string with the same contents) Looks kinda ugly though, any suggestions?
o
Generally, one may use shallowCopy() to create a copy, like in here https://github.com/JetBrains/kotlin-native/blob/master/backend.native/tests/runtime/workers/worker0.kt