-
Notifications
You must be signed in to change notification settings - Fork 115
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
Comments
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. |
@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 :( |
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. |
@jdm big thanks sir ! Really appreciated. |
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
The text was updated successfully, but these errors were encountered: