lib.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author notes
  8. * 2025-10-10 foxglove mutex test demo
  9. * 2025-10-29 foxglove Updated to demonstrate new modular macro interface
  10. */
  11. #![no_std]
  12. extern crate alloc;
  13. use alloc::sync::Arc;
  14. use core::time::Duration;
  15. use rt_macros::msh_cmd_export;
  16. use rt_rust::mutex::Mutex;
  17. use rt_rust::param::Param;
  18. use rt_rust::println;
  19. use rt_rust::thread;
  20. use rt_rust::time;
  21. #[msh_cmd_export(name = "rust_mutex_demo", desc = "Rust example app.")]
  22. fn main(_param: Param) {
  23. let counter = Arc::new(Mutex::new(0).unwrap());
  24. let run = move || loop {
  25. time::sleep(Duration::new(2, 0));
  26. {
  27. let mut c = counter.lock().unwrap();
  28. *c += 1;
  29. println!("{}", *c);
  30. }
  31. };
  32. let _ = thread::Thread::new()
  33. .name("thread 1")
  34. .stack_size(2048)
  35. .start(run.clone());
  36. time::sleep(Duration::new(1, 0));
  37. let _ = thread::Thread::new()
  38. .name("thread 2")
  39. .stack_size(2048)
  40. .start(run.clone());
  41. }