In my cocos2d game i have a some balls which must be destroyed, and there are 2 threads which are concurrent with each other, first thread add balls to the NSMutablearray, and second thread iterate through this array and calls release method for each ball,i have put every operation with array in synchronized block with @synchronized(array) but its not affect and every time in synchronized block application throws an exception __NSArrayM was mutated while being enumerated:
maybe there is other way to synchronize threads?
In my cocos2d game i have a some balls which must be destroyed, and
Share
Since you’re adding objects from one thread and iterating over the same array with another thread, it seems rather pointless to multithread this part of your code.
The reason is that you can not modify an array while iterating over it, regardless of whether you do it from within the same thread or multiple threads.
You will most likely get better results by using two arrays, one for each thread, and each thread perfoming the same tasks: both are adding objects, then both iterate over their half half of the objects. How you split the objects is up to you, it could be based on screen coordinates (screen split) or some other condition (ie balance number of objects processed by each thread).