0 votes

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?)

asked Mar 29, 2019 in Engine by Charlie (89 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.