Is it safe to modify Arc <Mutex <T>> from both the Rust thread and the external thread?
Are there any general rules, project documentation, or something similar that explains how the standard Rust library works with threads that were not spawned by std::thread ?
I have a cdylib box and want to use it from another language as threads:
use std::mem; use std::sync::{Arc, Mutex}; use std::thread; type jlong = usize; type SharedData = Arc<Mutex<u32>>; struct Foo { data: SharedData, } #[no_mangle] pub fn Java_com_example_Foo_init(shared_data: &SharedData) -> jlong { let this = Box::into_raw(Box::new(Foo { data: shared_data.clone() })); this as jlong } #[cfg(target_pointer_width = "32")] unsafe fn jlong_to_pointer<T>(val: jlong) -> *mut T { mem::transmute::<u32, *mut T>(val as u32) } #[cfg(target_pointer_width = "64")] unsafe fn jlong_to_pointer<T>(val: jlong) -> *mut T { mem::transmute::<jlong, *mut T>(val) } #[no_mangle] pub fn Java_com_example_Foo_f(this: jlong) { let mut this = unsafe { jlong_to_pointer::<Foo>(this).as_mut().unwrap() }; let data = this.data.clone(); let mut data = data.lock().unwrap(); *data = *data + 5; } specially in
let shared_data = Arc::new(Mutex::new(5)); let foo = Java_com_example_Foo_init(&shared_data); is it possible to change shared_data from the thread spawned by thread::spawn if Java_com_example_Foo_f is called from an unknown JVM thread?
+8
fghj
source shareNo one has answered this question yet.
See related questions:
1266
674
529
325
220
210
111
5
one
0