Browse Source

Added thread-basic example

pull/30/head
Andrea Ciliberti 3 years ago
parent
commit
5bdb0a4db1
  1. 47
      ctru-rs/examples/thread-basic.rs
  2. 3
      ctru-rs/src/thread.rs

47
ctru-rs/examples/thread-basic.rs

@ -0,0 +1,47 @@
use ctru::console::Console;
use ctru::gfx::Gfx;
use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, KeyPad};
use ctru::thread;
use std::time::Duration;
fn main() {
// Initialize services
ctru::init();
let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap();
let gfx = Gfx::default();
let _console = Console::init(gfx.top_screen.borrow_mut());
let prio = thread::current().priority();
println!("Main thread prio: {}\n", prio);
for ix in 0..3 {
thread::Builder::new()
.priority(prio - 1)
.spawn(move || {
let sleep_duration: u64 = 1000 + ix * 250;
let mut i = 0;
loop {
println!("Thread{ix} says {i}");
i += 1;
thread::sleep(Duration::from_millis(sleep_duration));
}
})
.unwrap();
println!("Created thread {ix}");
}
while apt.main_loop() {
gfx.flush_buffers();
gfx.swap_buffers();
gfx.wait_for_vblank();
hid.scan_input();
if hid.keys_down().contains(KeyPad::KEY_START) {
break;
}
}
}

3
ctru-rs/src/thread.rs

@ -10,8 +10,7 @@
//! 3DS-specific threading API //! 3DS-specific threading API
//! //!
//! While it is possible to create threads on the 3DS using functions found in //! The standard API does not expose the ability to set a thread's
//! `std::thread`, the standard API does not expose the ability to set a thread's
//! priority level and to pin a thread to a specific CPU core. This module exists //! priority level and to pin a thread to a specific CPU core. This module exists
//! to address those and other shortcomings. //! to address those and other shortcomings.
//! //!

Loading…
Cancel
Save