Roman Abaev
08/11/2024, 8:52 AMRoman Abaev
08/12/2024, 6:31 AMSam
08/12/2024, 7:03 AMsend
at the same time as the consumer is calling receive
. If the consumer gets there first, it suspends until the receiver arrives, and vice versa. There's no need for a buffer because each call to send
directly resumes a corresponding call to receive
.Roman Abaev
08/12/2024, 9:23 AMreceive
can read the value from the send
if there is no buffer?
by send
and receive
I mean the coroutine and I can't get how can it be expressed in code.
do coroutines use some variable outside of the buffer to exchange (read/write)?Sam
08/12/2024, 9:27 AMclass RendezvousChannel<T> {
private var receiver: Continuation<T>? = null
suspend fun receive(): T =
suspendCoroutine { continuation -> receiver = continuation }
fun send(value: T) {
receiver?.resume(value)
}
}
This code is very incomplete, because it only covers the scenario where the receiver arrives first, and it only allows for one sender and receiver at a time. But it should serve to demonstrate the basic principle.Roman Abaev
08/12/2024, 12:08 PMRoman Abaev
08/12/2024, 12:14 PM