Axstd mocks libstd APIs, and JoinHandle is documented as follows:
|
/// A `JoinHandle` *detaches* the associated thread when it is dropped, which |
|
/// means that there is no longer any handle to the thread and no way to `join` |
|
/// on it. |
|
pub struct JoinHandle<T> { |
|
native: AxTaskHandle, |
|
thread: Thread, |
|
packet: Arc<Packet<T>>, |
|
} |
It means when JoinHandle is dropped, the thread is still running behind the scenes. playground
However, ArceOS doesn't schedule the thread at all. Here's a demo:
# Cargo.toml
axstd = { workspace = true, features = ["multitask"] }
#![no_std]
#![no_main]
use axstd::{println, thread};
use core::sync::atomic::{AtomicU64, Ordering};
#[unsafe(no_mangle)]
fn main() {
let n = AtomicU64::new(0);
loop {
let id = n.fetch_add(1, Ordering::Relaxed);
_ = thread::spawn(move || println!("{id}")).join(); // (1)
// drop(thread::spawn(move || println!("{id}"))); // or (2)
if id == 10000 {
break;
}
}
println!("main stops.");
}
(1) works well because it waits for the task to finish, and all the 10000 threads are successfully run.
(2) causes OOM panic: no task is run at all, so memory runs out.
[ 0.029819 0:2 axruntime::lang_items:5]
panicked at library/alloc/src/alloc.rs:437:13: memory allocation of 262144 bytes failed
Axstd mocks libstd APIs, and JoinHandle is documented as follows:
arceos/ulib/axstd/src/thread/multi.rs
Lines 158 to 165 in 9b740db
It means when JoinHandle is dropped, the thread is still running behind the scenes. playground
However, ArceOS doesn't schedule the thread at all. Here's a demo:
(1) works well because it waits for the task to finish, and all the 10000 threads are successfully run.
(2) causes OOM panic: no task is run at all, so memory runs out.