This crate introduces the niche ThreadLock struct.
This struct stores arbitrary data, but only allows access to it from a specific thread at runtime; in exchange, the ThreadLock itself is Send and Sync.
This has very limited usage, but occasionally it may be useful for cases where some parts of a struct must be multithreaded, while other parts cannot be.
Often, these should be split into distinct structs (one of which is Sync while the other is not), but this may occasionally be a simpler option.
A (contrived) example similar to an actual usage I had:
; // A: Sync
;
The notable features of this example:
- I want to be able to do some of the things
ABcan do on all threads, so I wantABto beSync. - Some of the things AB can do (namely,
foo_and_bar) requireABto have resources (namely,B) that cannot be shared among threads, as well as the multi-threaded resources. AandBcan only be constructed together; this is less important, but it can make it harder or less ergonomic to splitABinto distinct structs.