Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code is safe so long as the calling code ensures that the object passed by reference is not destroyed during the execution of this function. This is the same for any function that takes a reference and you would have to work very hard to not do this.</p> <p>The destructor is safe because the atomic decrement is guaranteed to be zero in one and only one thread. And if it is, it must be that every other thread has already finished using the object and already invoked its own decrement operation.</p> <p>This assumes your interlocked operations all have full barriers.</p> <blockquote> <p>how could I get into a scenario where thread1 is copying and thread2 is destroying concurrently when the count == 1. There has to be a handshake between the threads, meaning thread1 has to copy the object entirely before thread2's object goes out of scope correct?</p> </blockquote> <p>You can't, so long as each thread has its own reference to the object. The object can't be destroyed while thread1 is copying so long as thread1 has its own reference to the object. Thread1 needn't copy before thread2's reference goes away because you would never, ever touch an object unless you had a reference to it.</p> <p>Individual references have weak thread safety and shouldn't be accessed in different threads concurrently. If two threads want to access the same object, they should each have their own reference. When giving a reference to an object to some other code (potentially in another thread), follow this sequence of operations:</p> <ol> <li><p>Have your own reference.</p></li> <li><p>Create the reference for the other code from your own reference.</p></li> <li><p>Now you may destroy your reference or give away the other reference.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload