You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pub struct Test {
sender: Sender<usize>
}
impl Test {
pub(crate) fn new() -> Self {
let (sender, _) = broadcast::channel::<usize>(100);
Self {
sender: sender,
}
}
async fn write(&self, buf: usize) {
self.sender.send(buf)
// Do other stuff
// Close the sender as if a condition was met
}
async fn read(&self) {
let mut receiver = self.sender.subscribe();
let value = receiver.recv();
// Do other stuff
}
}
Where:
We create a channel, saving the Sender as a struct variable.
We have a write method, called by only one thread/task, which uses the Sender to send values.
We have a read method, called by many threads/tasks, which subscribes to the Sender and waits for values to come.
The main concern is on line let value = receiver.recv();, if the writer ends and no more values are sent, but new readers come, that line will block forever, waiting for new values to appear in the broadcast channel.
The solution is to close the channel/sender, as I expect the lock to be released with a Closed message, but AFAIK the only way to close the Sender is to drop it.
As we have the Sender as an instance variable of the struct, I am not sure how to drop it, also, there is no close() method available that we can use instead of dropping the object.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everyone, I have the following pseudocode:
Where:
Sender
as a struct variable.write
method, called by only one thread/task, which uses theSender
to send values.read
method, called by many threads/tasks, which subscribes to theSender
and waits for values to come.The main concern is on line
let value = receiver.recv();
, if the writer ends and no more values are sent, but new readers come, that line will block forever, waiting for new values to appear in the broadcast channel.The solution is to close the channel/sender, as I expect the lock to be released with a
Closed
message, but AFAIK the only way to close theSender
is to drop it.As we have the Sender as an instance variable of the struct, I am not sure how to drop it, also, there is no
close()
method available that we can use instead of dropping the object.Any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions