cs431 icon indicating copy to clipboard operation
cs431 copied to clipboard

[Lecture] Type that implements Sync but not send

Open m-spitfire opened this issue 11 months ago • 3 comments

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.

m-spitfire avatar Mar 18 '24 04:03 m-spitfire

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.

Lee-Janggun avatar Mar 18 '24 05:03 Lee-Janggun

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.

m-spitfire avatar Mar 18 '24 06:03 m-spitfire

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).

Lee-Janggun avatar Mar 18 '24 06:03 Lee-Janggun