Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should we combine the Channel and the Sender types? #311

Open
marmistrz opened this issue Sep 13, 2023 · 5 comments
Open

Should we combine the Channel and the Sender types? #311

marmistrz opened this issue Sep 13, 2023 · 5 comments

Comments

@marmistrz
Copy link

When writing multithreaded core, using channels is quite messy. For each channel, you need to store both the channel (which you will never touch afterwards) and the sender (which is the one you use). In real-life examples, where CPU-intensive operation are triggered by the interactions with the UI, both need to be stored in the model. This feels quite messy.

Is there any reason against combining these two types into a single one?

@antoyo
Copy link
Owner

antoyo commented Sep 13, 2023

It's been a while since I wrote this, but I believe the Channel type cannot be sent to another thread. Have you tried to do so?

@marmistrz
Copy link
Author

glib::Source implements Send and so does relm::Channel. I checked it by adding a drop(channel) to the end of the spawned closure in multithread.rs and the example works perfectly well.

@antoyo
Copy link
Owner

antoyo commented Sep 14, 2023

Wouldn't that create a race condition where it could be possible that the channel is dropped before the other thread received the message?

The Channel is like a Receiver, so it is usual to have 2 halves when creating a channel. It's just that the one in relm doesn't have a recv() method and looks unused as a result.

@marmistrz
Copy link
Author

marmistrz commented Sep 14, 2023

Perhaps, I think about the following sequence:

  1. Sender sends
  2. Sender is dropped
  3. Channel is dropped
  4. Receiver tries to receive.

Would it make sense to possibly allow storing the channel within the relm context, given that in most usecases this will be the lifetime of the channel and that the channel is tied to the relm stream? fwiw, I think about Box::leaking the channel in my case so that I don't have to store a _channel per channel

@antoyo
Copy link
Owner

antoyo commented Sep 20, 2023

Would it make sense to possibly allow storing the channel within the relm context, given that in most usecases this will be the lifetime of the channel and that the channel is tied to the relm stream?

I'm not sure I understand what you suggest. Could you please provide a code example and what it would generate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants