`*mut c_void` cannot be sent between threads safely
within `shared_memory::Shmem`, the trait `Send` is not implemented for `*mut c_void`, which is required by `{closure@src\main.rs:86:32: 86:39}: Send`
required for `Mutex<shared_memory::Shmem>` to implement `Sync`
required for `Arc<Mutex<shared_memory::Shmem>>` to implement `Send`
required because it appears within the type `(std::string::String, Arc<Mutex<shared_memory::Shmem>>)`
required for `hashbrown::raw::RawTable<(std::string::String, Arc<Mutex<shared_memory::Shmem>>)>` to implement `Send`
The API looks like you are supposed to access shared memory regions via their ID. Here's a multi-threaded example linked from the official documentation.
Supposing you already looked at these examples, what's wrong with them?
In general, when you encounter a type that isn't Send, there are multiple possibilities for what the situation is:
The Shmem could just be Send, or Send + Sync, but the library didn't add the required unsafe impl Send to mark it so. In that case, a PR to the library would be appropriate.
The Shmem is not sound to send or share, but it could be — additional care would be needed. A less trivial modification.
The Shmem would be unsound if sent. In that case, you must create it on the thread it is to be used on. One useful technique here is the “actor”: create a thread to own the Shmem, and make use of it by sending messages over a channel from other threads that need to use it.
It's impossible to know which of these 3 cases applies without studying the actual code in the shared_memory library, and the system calls involved.
The problem is that I want to have program which serves this ID dynamically to any process that will request it. This serving functionality I want to have in separate thread of my program.
While writing it, I thought I need the instance of Shmem moved to the thread, but right now, when you responded, I think I can just move os_id which is String.
That example looks very worrying. It is using read_volatile and unsynchronized writes to write to the same piece of memory from multiple threads, resulting in multiple data races which are UB.
Yes, but that's another problem. (It even says that in the comments.) But it does demonstrate that the Shmem type itself is not to be shared but re-created based on its ID.