Hello I am reading about channels internals and qu...
# coroutines
r
Hello I am reading about channels internals and quite understood on high level how everything is working according to the document: https://arxiv.org/pdf/2211.04986 But I can't get how rendezvous is happening in the channels. How does coroutines(receiver and sender) exchange data without any intermediate buffer? Any link or code example is appreciated, thanks 🙂
c
This is the talk you're searching for 🙂

https://www.youtube.com/watch?v=ee0cG2SDJNEâ–¾

r
that's the video from where I got the link and found out about mentioned document. But what he is talking is that there is a two coroutines make a rendezvous and exchange data. Which is very abstract.
s
'Rendezvous' means that the two coroutines are in the same place at the same time. To put it more concretely, the producer is calling
send
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
.
r
Thx. That part I understood. But this is a theoretical part But corresponding receive should read the value and store from somewhere. In the case of rendezvous
receive
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)?
s
Well, here's a dramatically simplified example:
Copy code
class 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.
r
wow, thanks, yeah that is totally make sense now thanks a lot 🙂
forgot about Continuation.resumeWith and now I also found this in sourceCode -> was looking in the wrong direction once again: thanks a lot 🙂