https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

Animesh Sahu

01/16/2020, 6:44 AM
How do you suspend a bunch of coroutines, (may be under a supervisor or a context) till given amount of time, i.e. to make sure changes have been saved. I don't want to run any of the job under a given period say 5ms. There is no delay function in coroutine context or a supervisor job.
o

octylFractal

01/16/2020, 6:52 AM
your best bet is probably https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-semaphore/index.html or rolling your own similar construct. essentially since suspension is co-operative, i.e. you need the jobs to reach a suspend point in order to suspend them, you'll have to have the jobs "check-in" with some data structure, and when you want them to not run, that data structure needs to suspend them at that point, hold on to the Continuation, and resume it when you want them to run again
the way the Semaphore fits in to that is that the jobs can each take out a Semaphore, but when you want them to stop you have the "stopper" coroutine acquire every permit, then release them when time is up
the downside is that you need as many semaphores as you have concurrent coroutines
d

Dico

01/16/2020, 7:16 AM
Or you have a read/write lock construct, acquire the write lock to suspend all other coroutines that should each acquire the read lock, which is shared.
I dont know if coroutines has an equivalent of this 🤔
A non-blocking one, that is
o

octylFractal

01/16/2020, 7:18 AM
not in the base library, Mutex is as it says, Mutually Exclusive, no r/w version
a

Animesh Sahu

01/16/2020, 8:03 AM
How would coroutine suspend, do i need to acquire semaphore inside coroutine that is to be suspended and release from the other coroutine?
2 Views