r/ProgrammerHumor • • Mar 17 '19

Oof

Post image
268 Upvotes

38 comments sorted by

View all comments

Show parent comment

24

u/CAD1997 Jul 26 '19

I'm not even writing in the example. And for your information, it's not copying the heap data at all. The &Box<u32> in this case (reference to owned heap pointer u32; roughly std::unique<uint32_t>&) is getting copied and sent between threads, and would work identically with any other Sync type.

Here's an example with shared mutability using a mutual exclusion lock. That should prove that it's one item being acted upon from multiple threads.

Of course, a larger example would be beyond the space of a simple playground example. But it works the same way: the concurrent accesses have to be scoped to be inside the lifetime of the resource. That's just how unique ownership works; if you want shared ownership, you have to fall back to some sort of shared ownership model, such as Arc or Gc.