cs431
cs431 copied to clipboard
[Lecture] Type that implements Sync but not send
Upon googling the question I came across this question: https://users.rust-lang.org/t/sync-but-not-send/21551
Here's a real world example of type that is Sync
and !Send
:
I've heard that there are some ffi types which must be destroyed on the same thread on which they are created, but I can't give a concrete example. A contrived example would be a struct which, on creation, puts something to the thread local storage, and accesses that info in Drop.
https://docs.rs/zipkin/latest/zipkin/struct.Attached.html The crate tracks the current tracing context in thread local storage and restores the previous on drop, so it can't be moved across threads.
Do you have any questions on this? Or was this mentioned in the lecture, and you are posting this as a follow-up?
A more classical example is std's MutexGuard.
No it was brought up in the lecture, I'm just posting as follow-up.
The MutexGuard was also mentioned in the lecture, however professor stated that the !Send
on MutexGuard is usually too restrictive.
Ah ok, thanks for the follow up.
In terms of the MutexGuard, it has a very good reason for being !Send
, as you can read from my comment from two years ago (https://github.com/kaist-cp/cs431/issues/667#issuecomment-1274901668).