2 rust thread problems

⚓ Rust    📅 2026-06-18    👤 surdeus    👁️ 2      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: 2 rust thread problems
  1. rust thread is actually running in a real thread, or is it a virtual thread ?
  2. rust thread.sleep cause main thread sleeped.

my test code is

use std::thread;
use std::time::{Duration};
use chrono::Local;
struct HelloWorld { }

impl HelloWorld {
    fn output(&self) {
        println!("HelloWorld.output! current thread is {:?}. time is {}", thread::current().id(), Local::now().format("%H:%M:%S.%3f"));
        thread::sleep(Duration::from_secs(30));
        println!("after sleeping current thread is {:?}. time is {}", thread::current().id(), Local::now().format("%H:%M:%S.%3f"));
    }

    fn say_hello(&self) {
        thread::scope(|scope| {
           scope.spawn( || {
               self.output();
           });
        });

        println!("after Thread. current thread is {:?}. time is {}", thread::current().id(), Local::now().format("%H:%M:%S.%3f"));
    }
}

fn main() {
    println!("Hello, main! current thread is {:?}, time is {}", thread::current().id(), Local::now().format("%H:%M:%S.%3f"));
    let hw = HelloWorld {};
    hw.say_hello();
    println!("after. thread is {:?}, time is {}", thread::current().id(), Local::now().format("%H:%M:%S.%3f"));
}

click here to view test code runs video.

QQ20260618-112546

-------- update code with mio udp server test ---------

Cargo.toml

[dependencies]
chrono = "0.4.44"
mio = {version = "1.2.0", features = ["net", "os-poll", "os-ext"]}

main.rs

use chrono::Local;
use std::thread;
use std::time::Duration;
use mio::{event::Event, net::UdpSocket, Events, Interest, Poll, Token};
use std::{
    io::{self, ErrorKind::WouldBlock},
    str::from_utf8,
};

const SERVER: Token = Token(0);

struct HelloWorld {}

impl HelloWorld {
    fn output(&self) {
        println!(
            "HelloWorld.output! current thread is {:?}. time is {}",
            thread::current().id(),
            Local::now().format("%H:%M:%S.%3f")
        );
        thread::sleep(Duration::from_secs(30));
        println!(
            "after sleeping current thread is {:?}. time is {}",
            thread::current().id(),
            Local::now().format("%H:%M:%S.%3f")
        );
    }

    fn say_hello(&self) {
        thread::scope(|scope| {
            scope.spawn(|| {
                self.output();
            });

            print!("say_hello ???-----");
        });

        println!(
            "after Thread. current thread is {:?}. time is {}",
            thread::current().id(),
            Local::now().format("%H:%M:%S.%3f")
        );
    }

    fn handle(&self, event: &Event, server: &UdpSocket) -> io::Result<()> {
        if event.token() != SERVER {
            return Ok(());
        }

        let mut buffer = vec![0; 4096];
        loop {
            match server.recv_from(&mut buffer) {
                Ok((size, address)) => {
                    println!("客户端: {}", address);
                    let received = &buffer[..size];
                    let str = from_utf8(received).unwrap();
                    println!("收到数据:{}", str);
                    server.send_to(str.to_ascii_uppercase().as_bytes(), address)?;
                }
                Err(e) if e.kind() == WouldBlock => break,
                Err(err) => return Err(err),
            }
        }
        Ok(())
    }

    fn start(&self) -> io::Result<()> {
        let addr = "127.0.0.1:4444".parse().unwrap();
        let mut server = UdpSocket::bind(addr)?;

        let mut poll = Poll::new()?;
        let mut events = Events::with_capacity(128);
        poll.registry()
            .register(&mut server, SERVER, Interest::READABLE)?;

        loop {
            poll.poll(&mut events, None)?; <---- here
            for event in events.iter() {
                self.handle(event, &server)?;
            }
        }
    }

    fn test_start(&self) {
        thread::scope(|scope| {
            scope.spawn(|| {
                let t = self.start();
            });
        });
    }
}

fn main() {
    println!(
        "Hello, main! current thread is {:?}, time is {}",
        thread::current().id(),
        Local::now().format("%H:%M:%S.%3f")
    );
    let hw = HelloWorld {};
    hw.test_start();
    println!(
        "after. thread is {:?}, time is {}",
        thread::current().id(),
        Local::now().format("%H:%M:%S.%3f")
    );
}

run the code , it will be blcoked at
poll.poll(&mut events, None)?;
I want function start runs in a thread. if using scope , main thread should wait , if using thread::spawn , self should be borrowed .
how to fix it?

9 posts - 4 participants

Read full topic

🏷️ Rust_feed