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

ursus

11/12/2019, 6:03 PM
Hi, if I for loops async { }, is there a guarantee of order of them starting to run? I have a semaphore inside them, but it seems race-y
o

octylFractal

11/12/2019, 6:09 PM
No, I don't think there is a guaranteed order
z

Zach Klippenstein (he/him) [MOD]

11/12/2019, 6:33 PM
Nope, there is no ordering guarantee. If you need explicit ordering though, you can implement it manually.
u

ursus

11/12/2019, 6:37 PM
Ok makes sense, how would I go about doing that? Blocking queue?
z

Zach Klippenstein (he/him) [MOD]

11/12/2019, 6:45 PM
If by “blocking queue” you mean
Channel
, then yes, that’s one way 😉
(
Channel
is effectively a blocking suspending queue)
u

ursus

11/12/2019, 7:33 PM
sorry im coroutine newb. Ive seen channels used to pipe data, but it seems I want to pipe lazy Deffered instances and start them in the receiver?
z

Zach Klippenstein (he/him) [MOD]

11/12/2019, 7:54 PM
You could do that, or you could just send suspend functions through your channel directly, and have your receiver do the
async
part
u

ursus

11/12/2019, 8:44 PM
hm, but doesnt that make the start of the async race-y again?
z

Zach Klippenstein (he/him) [MOD]

11/12/2019, 8:49 PM
true.
start = UNDISPATCHED
might be better here, no channel required,
u

ursus

11/12/2019, 9:31 PM
interesting, the way I read the docs is the async body going to actually get executed synchronously until first susp. point, correct?you think I can rely on that?
z

Zach Klippenstein (he/him) [MOD]

11/12/2019, 10:49 PM
yep
4 Views