I want to create a real time game. The game will have a model which will be updated periodically…
- (void) timerTicks {
[model iterate];
}
Like all games, I will revive user input events, touches. In response I will need to update the model….
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[model updateVariableInModel];
}
So there are two threads:
- From a timer. Frequently iterates the model
- From the UI thread. Updates the the model based on user input
Both threads will be sharing variables in the model.
What is the best practice for sharing the objects between the threads and avoiding multi threading issues?
Lock the objects that need to be shared across the thread by using the @synchronized keyword.
An easy way to lock all objects is like this:
For more info on threading in objective-c, please go here
Also note that you must lock the same object both times, or else the lock itself is useless.