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?

A possible reason this could be bad.

+8
multithreading rust
source share

No one has answered this question yet.

See related questions:

1266
How to update GUI from another thread?
674
What is a thread safe or insecure thread in PHP?
529
Invalid cross-thread operation: control is from a thread other than the thread that was created on
325
What is meant by thread safe code?
220
Obtaining a stream identifier from a stream
210
Is C # static constructor thread safe?
111
Rust package with library and binary?
5
How to lend a Rust object to C code for an arbitrary lifetime?
one
How exactly does stack and heap distribution work in Rust?
0
Is it safe to use Mutex without an arc in multithreading?

All Articles