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

Support for multithreading #3

Open
lovelyjuice opened this issue Aug 9, 2024 · 0 comments
Open

Support for multithreading #3

lovelyjuice opened this issue Aug 9, 2024 · 0 comments

Comments

@lovelyjuice
Copy link

// examples/ping.rs

use std::io;
use std::net::IpAddr;
use std::time::Duration;

use futures::{channel::mpsc, StreamExt};
use ping_async::IcmpEchoRequestor;

use tokio::time;

#[tokio::main]
async fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() != 2 {
        eprintln!("Usage: ping <destination>");
        std::process::exit(1);
    }

    let destination = args[1].parse().unwrap();
    tokio::spawn(async move {
        let _ = ping(destination, 1).await;
    });
}

async fn ping(dest: IpAddr, times: usize) -> io::Result<()> {
    let (tx, mut rx) = mpsc::unbounded();

    let pinger = match IcmpEchoRequestor::new(tx, dest, None, None, None) {
        Ok(req) => req,
        Err(e) => {
            eprintln!("Error in new: {}", e);
            return Err(e);
        }
    };

    for _ in 0..times {
        if let Err(e) = pinger.send().await {
            eprintln!("Error in send: {}", e);
            return Err(e);
        }

        match rx.next().await {
            Some(reply) => {
                println!(
                    "Reply from {}: status = {:?}, time = {:?}",
                    reply.destination(),
                    reply.status(),
                    reply.round_trip_time()
                );
            }
            None => {
                eprintln!("channel is closed.");
            }
        }

        time::sleep(Duration::from_secs(1)).await;
    }

    Ok(())
}

Error with:

error: future cannot be sent between threads safely
   --> src\main.rs:21:5
    |
21  | /     tokio::spawn(async move {
22  | |         let _ = ping(destination, 1).await;
23  | |     });
    | |______^ future created by async block is not `Send`
error[E0277]: `NonNull<Arc<std::sync::Mutex<ping_async::platform::windows::ReplyContext>>>` cannot be shared between threads safely
   --> src\main.rs:21:5
    |
21  | /     tokio::spawn(async move {
22  | |         let _ = ping(destination, 1).await;
23  | |     });
    | |______^ `NonNull<Arc<std::sync::Mutex<ping_async::platform::windows::ReplyContext>>>` cannot be shared between threads safely
    |
    = help: within `IcmpEchoRequestor`, the trait `Sync` is not implemented for `NonNull<Arc<std::sync::Mutex<ping_async::platform::windows::ReplyContext>>>`, which is required by `{async block@src\main.rs:21:18: 23:6}: std::marker::Send`
note: required because it appears within the type `IcmpEchoRequestor`
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

1 participant