erickt / rust-zmq

Rust zeromq bindings.
Apache License 2.0
886 stars 189 forks source link

Make Socket Send and Sync #348

Closed divyekapoor closed 1 year ago

divyekapoor commented 1 year ago

Without this change, trying to access a Socket in a global / static variable does not work even if a single thread is actually accessing the socket.

error[E0277]: *mut c_void cannot be shared between threads safely --> src/lib.rs:49:30 49 let nettle_globals = globals::get::(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *mut c_void cannot be shared between threads safely

= help: within (NettleHandle, NettleOperator), the trait Sync is not implemented for *mut c_void = note: required because it appears within the type Socket note: required because it appears within the type NettleOperator --> src/lib.rs:23:8

Jasper-Bekkers commented 1 year ago

Don't share ZeroMQ sockets between threads. ZeroMQ sockets are not threadsafe. Technically it's possible to migrate a socket from one thread to another but it demands skill. The only place where it's remotely sane to share sockets between threads are in language bindings that need to do magic like garbage collection on sockets.

Reference: https://zguide.zeromq.org/docs/chapter2/