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

Send message to a specific thread #243

Closed
KabDeveloper opened this issue Sep 20, 2019 · 4 comments
Closed

Send message to a specific thread #243

KabDeveloper opened this issue Sep 20, 2019 · 4 comments

Comments

@KabDeveloper
Copy link

Hi

Is there any clear example on how to send a message to a specific thread ?

Clearely, I want a function inside a thread that listen live messages received from the main function of my script on user request, this mean each time a request is receive from online user, I send it to the specific thread were the function is working.

Just a small example that interpret this case please, I am stcuking since I am new to channels ad can't find a good example that demonstrating how it works.

Thanks again

@jdm
Copy link
Member

jdm commented Sep 20, 2019

Messages are not sent to threads in this library; they are sent to a receiver. To have a specific thread receive a message, you need to create a sender and receiver pair (ipc::channel()), then move the receiver into the thread when it is spawned and use it to receive messages.

@KabDeveloper
Copy link
Author

@jdm Thank you so much for your answer !

Is there a way you show a simple example on how to achieve that please ? I can't find any in your github :(

@jdm
Copy link
Member

jdm commented Sep 20, 2019

Often you will have code that looks like this:

let (sender, receiver) = ipc::channel().unwrap();
thread::spawn(move || {
    while let Ok(msg) = receiver.recv() {
        // do something in response to receiving `msg`
    }
});

// We send integers here, but this could also be
// an enum, or strings, or something else.
let _ = sender.send(5);
// ...later...
let _ = sender.send(3);

That being said, it sounds like you might be looking for https://doc.servo.org/std/sync/mpsc/index.html rather than ipc-channel, since ipc-channel is used to support inter-process communication. If you're only communicating between threads, ipc-channel is an unnecessary burden.

@KabDeveloper
Copy link
Author

@jdm big thanks sir ! Really appreciated.

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