From 0a7dcb65317a7df46ce8e0ad1e53436df133cdb0 Mon Sep 17 00:00:00 2001 From: meloalright Date: Sat, 16 Nov 2024 17:12:34 +0800 Subject: [PATCH] feat(example): add examples of async --- examples/arc.rs | 29 +++++++++++++++++++++++++ examples/async.rs | 0 examples/{hello.rs => demo.rs} | 0 examples/mutex.rs | 38 +++++++++++++++++++++++++++++++++ examples/rwlock.rs | 39 ++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++++ 6 files changed, 110 insertions(+) create mode 100644 examples/arc.rs delete mode 100644 examples/async.rs rename examples/{hello.rs => demo.rs} (100%) create mode 100644 examples/mutex.rs create mode 100644 examples/rwlock.rs diff --git a/examples/arc.rs b/examples/arc.rs new file mode 100644 index 0000000..dfdd5ed --- /dev/null +++ b/examples/arc.rs @@ -0,0 +1,29 @@ +use std::thread; +use std::sync::{Arc}; +use taitank_safe::*; + +fn main() { + let mut root = node_create(); + set_align_items(&mut root, FlexAlign::FlexAlignCenter); + set_width(&mut root, 200.0); + set_height(&mut root, 200.0); + + layout!(&mut root); + + /// Arc + let arc_root = Arc::new(root); + + let mut handles = vec![]; + + for i in 0..5 { + let arc_root = Arc::clone(&arc_root); + let handle = thread::spawn(move || { + println!("{}", i); + println!("{:?}", arc_root); + }); + handles.push(handle); + } + for handle in handles { + handle.join().unwrap(); + } +} \ No newline at end of file diff --git a/examples/async.rs b/examples/async.rs deleted file mode 100644 index e69de29..0000000 diff --git a/examples/hello.rs b/examples/demo.rs similarity index 100% rename from examples/hello.rs rename to examples/demo.rs diff --git a/examples/mutex.rs b/examples/mutex.rs new file mode 100644 index 0000000..51468ea --- /dev/null +++ b/examples/mutex.rs @@ -0,0 +1,38 @@ +use std::thread; +use std::sync::{Arc, Mutex, RwLock}; +use taitank_safe::*; + +fn main() { + let mut root = node_create(); + set_align_items(&mut root, FlexAlign::FlexAlignCenter); + set_width(&mut root, 200.0); + set_height(&mut root, 200.0); + + layout!(&mut root); + + /// Mutex + let arc_mutex_root = Arc::new(Mutex::new(root)); + let mut handles = vec![]; + + for i in 0..5 { + let arc_mutex_root = Arc::clone(&arc_mutex_root); + let handle = thread::spawn(move || { + // 获取 Mutex 的锁并修改数据 + let mut root = arc_mutex_root.lock().unwrap(); + let width = get_width(&root); + set_width(&mut root, width + 10.0); + layout!(&mut root); + println!("Mutex {}", i); + println!("{:?}", root); + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + let final_data = arc_mutex_root.lock().unwrap(); + println!("Mutex final"); + println!("{:?}", final_data); +} \ No newline at end of file diff --git a/examples/rwlock.rs b/examples/rwlock.rs new file mode 100644 index 0000000..a4bf164 --- /dev/null +++ b/examples/rwlock.rs @@ -0,0 +1,39 @@ +use std::thread; +use std::sync::{Arc, Mutex, RwLock}; +use taitank_safe::*; + +fn main() { + let mut root = node_create(); + set_align_items(&mut root, FlexAlign::FlexAlignCenter); + set_width(&mut root, 200.0); + set_height(&mut root, 200.0); + + layout!(&mut root); + + /// RwLock + let arc_rwlock_root = Arc::new(RwLock::new(root)); + let mut handles = vec![]; + + for i in 0..5 { + let arc_rwlock_root = Arc::clone(&arc_rwlock_root); + let handle = thread::spawn(move || { + let root = arc_rwlock_root.read().unwrap(); + let width = get_width(&root); + drop(root); + let mut root = arc_rwlock_root.write().unwrap(); + set_width(&mut root, width + 10.0); + layout!(&mut root); + println!("RwLock {}", i); + println!("{:?}", root); + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + let final_data = arc_rwlock_root.read().unwrap(); + println!("RwLock final"); + println!("{:?}", final_data); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index cbc5c13..c056b7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,10 @@ impl Debug for TaitankSafeNode { } } +unsafe impl Send for TaitankSafeNode {} +unsafe impl Sync for TaitankSafeNode {} + + #[repr(i32)] pub enum Direction { Inherit = 0,