Hey all, question for a coroutine novice, I have ...
# coroutines
c
Hey all, question for a coroutine novice, I have a method that can be called concurrently and if a certain condition is met, I’d like to start a job with the first method that sees the condition is met, and then have the other threads wait for the result of that job. My first thought was to have some Atomic Semaphore / Lock that indicates one of the threads has claimed this job, but that would not allow for me to join on that result. What is the idiomatic way to do this in kotlin?
z
you could use the coroutines mutex/semaphore types, it would probably be the simplest
there are many more complicated ways to do stuff if you want to over-engineer
c
Thanks for your help
Would I be able to get the result of the job being awaited on?
My understanding of a semaphore is that is just locks and can be awaited on, but the other threads would only be aware that the lock has been lifted
z
If you’re using a synchronization primitive, you can communicate the result however you want (e.g. a shared class property). You could also try using something like
CompletableJob
, although that’s not as great for the synchronization use case you described
c
I think this is similar to what you want: https://gitlab.com/wildfyre/lib/-/blob/master/src/commonMain/kotlin/utils/SemaphoreCache.kt However that code is not new anymore, so it might not be ‘best practices'
t
@Cody Mikol if I understand your question correctly, we accomplished what you're describing using a combination of
atomic
and `async`: https://github.com/JuulLabs/kable/blob/f02748b0a1191d236a4f703c1385e834f3de9061/core/src/androidMain/kotlin/Peripheral.kt#L215