I’m doing some heavy loading and a few calculations for my GTK application. To not stall the event loop, I have to move these on to a separate thread. I know I can do a few things on the event loop using async
but this only got me so far and was still slowing everything down (my calculations were slow because of the constant interruptions and the animation was still choppy because of the calculations).
The main problem boils down to the fact that objects like gdk_pixbuf::Pixbuf
or poppler::PopplerDocument
are not Send
. I understand that this is not a simple problem because the structures are (non-atomically) reference counted. Here’s the solutions I’ve already come up with:
- Do everything on one thread in background tasks using
async
. - Use
unsafe
and send the object over anyway if I know I’m the exclusive owner. YOLO! - Serialize all data into a
vec
and then deserialize on the other side. (YMMV depending on the data, but images and PDF documents should work fine.)
None of these feels good. I’d really need some “official”, supported and built-in mechanism to somehow do these things with less pain. Some random ideas off the shelf:
- Provide some owned variants for all objects without that reference counter.
- Create a safe
Send
wrapper that fails if the data has RC > 1. - Create a safe
Send
wrapper that deep-clones the data where needed to ensure ownership - Make the reference counters atomic and then add some mutex mechanism (Probably not a realistic one).