Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my Cocos2D game for iOS I have a relatively long running calculation that happens at a fairly regular interval (every 1-2 seconds). I'd like to run the calculation on a background thread so the main thread can keep the animation smooth.

The calculation is done on a grid. Average grid size is about 100x100 where each cell stores an integer. Should I copy this grid when I pass it to the background thread? Or can I pass a reference and just make sure I don't write to the grid from the main thread before the background thread is done?

Copying seems a bit wasteful but passing a reference seems risky. So I thought I'd ask.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

If you can guarantee that no writing will be done to the grid during this process that will cause any deadlocks or race conditions or invalidate the calculation, then pass the reference. But I'm guessing you can't guarantee this, so copying the grid is usually worth it.

share|improve this answer
    
+1 Thanks for the response. If I have an NSArray *myArray that contains instances of NSArray will doing [myArray copy] suffice? I'm not sure of the deep vs shallow copy issues. –  SundayMonday Oct 22 '12 at 11:30
1  
I'm willing to bet that the copy message doesn't do deep-copying, and that your NSArray will likely only be storing pointers, unaware of the fact that more content needs to be copied. The Apple docs mention this copyWithZone message, which suggests to me that it's not unlike a memcpy, which definitely won't do any deep copying for you. –  michael.bartnett Oct 22 '12 at 17:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.