rsmgclient icon indicating copy to clipboard operation
rsmgclient copied to clipboard

Place mutex on Connection

Open cts660 opened this issue 2 years ago • 6 comments

I would like to use a connection to MemGraph in rust as a state in different functions, however when I tried to place a mutex on a connection to share it safely, the compiler indicated that `Sync` is not implemented for `*mut rsmgclient::bindings::mg_session. Is there any other way to safely share a MemGraph connection across multiple threads? Thank you!

cts660 avatar Sep 09 '23 20:09 cts660

you can create a wrapper and force it to be send, it's just dangerous if they are not properly handling the raw pointer inside the library

struct SendSync<T>(T);

unsafe impl<T> Send for SendSync<T> {}
unsafe impl<T> Sync for SendSync<T> {}

impl<T> SendSync<T> {
    fn new(value: T) -> Self {
        Self(value)
    }
}

impl<T> core::ops::Deref for SendSync<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

let connection = SendSync::new(connection);

kkuriboh avatar Oct 10 '23 14:10 kkuriboh

Hi @cts660, did the above tip help you? Did you manage to safely share a MemGraph connection across multiple threads?

katarinasupe avatar Dec 19 '23 12:12 katarinasupe