davegl1234
davegl1234

Reputation: 1

Objective C- Adding an object to an array, whilst mutating that object

In objective C, is it thread safe to add an object to an array (all processing/mutating/enumeration of this array occurs on its own single thread), whilst the object itself could potentially be mutated on a different thread?

When adding an object to an array, am I simply passing in a memory reference, and changes to the actual object will not cause issues at this point? Or will mutating that object on a different thread at the same time as adding it to an array result in a crash?

Thanks

Upvotes: 0

Views: 115

Answers (2)

Avinash Jadhav
Avinash Jadhav

Reputation: 501

Yes, it is thread safe.

  • To make thread safe array or any object, you must create object with property list "atomic".
    By this you will ensure that one thread will have access to your object / array and app won't crash.
  • If you are storing object reference in array, then mutating the object values won't crash. As array is pointing to objects, but all objects must be atomic whose reference you have stored in array. To achieve thread safe.

Upvotes: -1

gnasher729
gnasher729

Reputation: 52548

Yes, it's safe. The array only cares about storing a reference to the object. Any changes to the object itself are totally invisible to the array. Of course if one thread reads myObject = myArray [i] then that thread must be aware that the contents of the object could change at any time.

Upvotes: 2

Related Questions