2021-01-05 22:20:08 +00:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct NotThreadSafe {
|
2023-02-22 21:46:42 +00:00
|
|
|
data: Rc<i32>,
|
2021-01-05 22:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2023-02-22 21:46:42 +00:00
|
|
|
let obj = Python::with_gil(|py| {
|
2024-03-03 14:47:25 +00:00
|
|
|
Bound::new(py, NotThreadSafe { data: Rc::new(5) })
|
2023-02-22 21:46:42 +00:00
|
|
|
.unwrap()
|
2024-03-03 14:47:25 +00:00
|
|
|
.unbind(py)
|
2023-02-22 21:46:42 +00:00
|
|
|
});
|
2021-01-05 22:20:08 +00:00
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
2023-02-22 21:46:42 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
// Uh oh, moved Rc to a new thread!
|
2024-03-03 14:47:25 +00:00
|
|
|
let c = obj.bind(py).downcast::<NotThreadSafe>().unwrap();
|
2021-01-05 22:20:08 +00:00
|
|
|
|
2023-02-22 21:46:42 +00:00
|
|
|
assert_eq!(*c.borrow().data, 5);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.join()
|
|
|
|
.unwrap();
|
2021-01-05 22:20:08 +00:00
|
|
|
}
|