Can `Pin` bring back `thread::scoped` (and the drop guard pattern in general)?

The old thread::scoped was unsound because the JoinGuard could be moved somewhere else and forgotten without unsafe code.

Pin can be used to make a struct unmovable. So shouldn't it be possible to create a join guard that cannot be safely forgotten?

1 Like

No, because Pin doesn't require you to not forget the value.

Trivially:

let b = Box::pin(value);
mem::forget(b);

What Pin specifically requires is that the destructor is called before the location is made invalid. If you just leak the value, then the location is never made invalid, and so it's not invalidating the pin.

(Because of async fn, this even applies to stack values in macros; you only get the pinning guarantees.)

But scoped threads are making a return to std finally, using the crossbeam closure API.

7 Likes

I was thinking something like you can't Box::pin(value) because value is not movable.
But turns out my understanding of pin was wrong. It doesn't really allow creation of immovable types, only pointers to such.

The problem with Pin is that while it provides a drop guarantee, the drop guarantee applies to the wrong thing.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.