0

I was gonna create a Pyo3 library which implements some RL environments. I had been using Pyo3 for other tasks. I was gonna use also SDL2 rendering, however it gives me an error.

#[pyclass]
pub struct FirstEnv {
   pos: f32,
   renderer: Renderer,
}

pub struct Renderer {
    canvas: sdl2::render::WindowCanvas,
}

The error: Rc<RendererContext<WindowContext>> cannot be sent between threads safely

Is there a fix for this ? Or this kind of a combo will not work?

It shows me that within FirstEnv, the trait Send is not implemented for Rc<RendererContext<WindowContext>>. As I am new in rust tried googling and it says that it is not thread safe. However, I am not sure what should I do.

  • 1
    Try `Rc`s atomically counted cousin [`Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) – cafce25 Apr 19 '23 at 08:53
  • 1
    @cafce25 The `Rc` is inside [`WindowCanvas`](https://docs.rs/sdl2/0.35.2/src/sdl2/render.rs.html#346-350). So it's not like Edvards can just switch that. (I don't think wrapping the `WindowCanvas` into an `Arc>` would help either, because `Mutex` won't be `Send` if `T` isn't. But I might be wrong.) – Caesar Apr 19 '23 at 10:00
  • Yes, I tried wrapping it in `Arc>` it still gave me the same error. – Edvards Zakovskis Apr 19 '23 at 10:24
  • 1
    Ultimately, you may have to use a bigger hammer: Have a rendering thread that has exclusive access to the `WindowCanvas`, and pass messages to that thread (through `crossbeam` or similar). – Caesar Apr 19 '23 at 10:37

0 Answers0