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:

  1. from timer. iterates model
  2. 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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -