I have a simple iPhone game consisting of two “threads”: the main game loop where all updating and rendering happen 30 times per second (NSTimer)… and the “thread” that calls the accelerometer delegate 100 times per second. I have a variable “xPosition” that’s updated in the accelerometer delegate function and used in the game loop. Is there a possibility of the two “threads” trying to use xPosition at the same time (hence causing a crash or some other problem). If so how can I fix this w/ minimal impact to the game’s performance?
I’ve been using this set-up for many months of development and incremental testing and I’ve never run into any problems.
Cheers!
If your
NSTimertask and your game loop are both run from the main thread you will not encounter any problems with this since only one of them will execute at the same time. Additionally none of them can preempt the other.However if you are using different threads, you have to be careful when using the
xPositionin the game loop since it’s value might be updated at any time from the other thread – even though there is only one processor. One simple way of getting past this would be to assign the value ofxPositionto a local variable in the game loop and only reference this variable for each run through the loop.