Does array.duplicate() provide thread-safety when resizing, sorting, etc.? Or is it even needed?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Charlie

I have a thread that operates on an array, frequently sorting and sometimes resizing it. I want to access the contents of that array at any point in time from the main thread for read-only purposes. And I’d like to do this without locks.

I thought it might be thread-safe if the main thread performs array.duplicate(), then accesses (iterates, but doesn’t change) the duplicated array. Is this correct?

Zylann’s answer to a related question leads me to think that the duplication happens anyway (under the hood) whenever 2 threads access an arrray, so that in theory I don’t even need to do the duplicate() explicitly. It’s puzzling for me to understand how this could work though. How would the engine know which array is supposed to be the “original” if, say, I modified it at the same time from both threads? (Which I’m not doing!!! But what if I did?)