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

David Glasser

03/09/2020, 5:42 PM
If I have a
Copy code
Channel<Unit>(capacity = Channel.UNLIMITED)
and I never close it, is it guaranteed that
offer
will always succeed?
z

zak.taccardi

03/09/2020, 5:42 PM
yes, until you hit a memory issue
or if the scope is cancelled
oops, channels don’t have a scope haha
d

David Glasser

03/09/2020, 5:43 PM
and specifically, if I call
receive()
at most once, I should be in good shape?
(basically just trying to make a latch where one coroutine can
wait
on it, and various other places (which aren't all coroutines) can allow it to continue)
z

zak.taccardi

03/09/2020, 5:44 PM
receive like in a while loop?
d

David Glasser

03/09/2020, 5:45 PM
nah, just once
z

zak.taccardi

03/09/2020, 5:45 PM
why not use a deferred then?
d

David Glasser

03/09/2020, 5:45 PM
just like a one-time synchronization primitive, "wait until some other coro lets me continue"
well, it's fulfillable by multiple other places
this is like "let the main loop finish if any of these various places tell us the program should be over", that sort of thing
z

zak.taccardi

03/09/2020, 5:46 PM
CompletableDeferred<Unit>()
should be effectively the same thing but with the guarantee it can only emit once
d

David Glasser

03/09/2020, 5:47 PM
with
.complete(Unit)
and
.await()
?
z

zak.taccardi

03/09/2020, 5:47 PM
yeah
d

David Glasser

03/09/2020, 5:47 PM
and i can just do
parent = null
happily?
z

zak.taccardi

03/09/2020, 5:48 PM
not sure what you mean by parent
d

David Glasser

03/09/2020, 5:48 PM
initialize it with
CompletableDeferred<Unit>(parent = null)
rather than somethign else. this is all kinda at top program level
z

zak.taccardi

03/09/2020, 5:48 PM
even if you are top level, you still have a top level scope
d

David Glasser

03/09/2020, 5:49 PM
so i'm ok with no cancellation happening (in fact, i would rather not think about this Deferred being cancellable)
z

zak.taccardi

03/09/2020, 5:50 PM
oh yeah,
parent
is
null
by default - that’s fine
since you are synchronizing between 2 coroutines
4 Views