Changing Array From Thread

I’m just curious as to the ability, issues, and practicality of having multiple threads utilize the same array or property in general.

  1. Say I create a Queue() array and thread A adds items to the bottom of the array and thread B takes items off the top of the array. Is there any risk in doing this? What if I’m using foreach? If I’m using For with an iterator is it safe? What errors would/could get thrown? What do I do to prevent issues?

  2. Say thread A reads a string and then concatenates it on itself. Say thread B alters the string. Is it possible for thread B to alter the string inbetween thread A reading the string and thread A doubling it. Under what circumstances would this be true or avoided?
    like:

[code]
someGlobalString = “oldValue”

THREAD A
dim myString as string = someGlobalString
myString = myString + " " myString
someGlobalString = myString

THREAD B
someGlobalString = “newValue”[/code]

Would I ever have to worry about someGlobalString = “oldValue newValue” ???

Only one line of Xojo code can execute at a time. In your examples, there is no risk. However with loops, it does become possible for an array to be modified while the loop is processing it. If you worry about this, you can either DisableBackgroundTasks to prevent Xojo from yielding to other threads at loop boundaries, or use a CriticalSection to lock access to a single thread. Of the two, I would recommend the CriticalSection approach.