iphone - What is the best practice for sharing objects between threads for a real time game -
i want create real time game. game have model updated periodically...
- (void) timerticks { [model iterate]; }
like games, revive user input events, touches. in response need update model....
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { [model updatevariableinmodel]; }
so there 2 threads:
- from timer. iterates model
- from ui thread. updates the model based on user input
both threads sharing variables in model.
what best practice sharing objects between threads , avoiding multi threading issues?
lock objects need shared across thread using @synchronized keyword.
an easy way lock objects this:
-(void) iterate { @synchronized(self) { // thread safe } } -(void) updatevariableinmodel { @synchronized(self) { // use variable pleased, don't worry concurrent modification } }
for more info on threading in objective-c, please go here
also note must lock same object both times, or else lock useless.
Comments
Post a Comment