Hacker News new | ask | show | jobs
by mihaigalos 1414 days ago
So how do you implement multithreading if the above are a bad idea?
2 comments

The thread lifetime is also scope-bound, to ensure the thread dies before any object it might be referring to does.
Sorry, I meant how to guard against race conditions where multiple threads try to write to the same shared memory?

This is clasically done via memory synchronization mechanisms/atomics.

I don't understand how this has anything to do with lifetime management, which is what GC/RC/ARC are for.

You do need to synchronize concurrent access to the same object, and it's not just concurrent writes, it's concurrent accesses if at least one of them is a write.

Deep copy on passing between threads. Interesting pros and cons to the performance of that vs sharing pointers.
There is no need to copy objects for multiple threads to refer to them.

It's just that there is a single owner, and the other threads have a non-owning view only.

You need strict control on the thread lifetime, which is enforced through the same single ownership system, in order to enforce this.