iphone - How to wait in NSThread until some event occur in iOS? -


how wait inside nsthread until event occur in ios?

eg, created nsthread , started thread loop. inside thread loop, there condition check whether message queue has messages. if there message, call corresponding method operation, else should wait until message queue gets populated new message.

is there api or methods available wait until event occur?

for example   nsthread *thread = [nsthread alloc]....@selector(threadloop)  - (void)threadloop {    // expecting api or method wait until messages pushed message queue    if (...) {     } } 

any should appreciated.

you can use nscondition. attach example code "ready-for-test" in viewcontroller

@interface viewcontroller ()  @property (strong, nonatomic) nscondition *condition; @property (strong, nonatomic) nsthread *athread;  // use property indicate want lock _athread @property (nonatomic) bool lock;  @end  @implementation viewcontroller  - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.      // start thread locked, update boolean var     self.lock = yes;      // create nscondition instance     self.condition = [[nscondition alloc]init];      // create thread , start     self.athread = [[nsthread alloc] initwithtarget:self selector:@selector(threadloop) object:nil];     [self.athread start];  }  -(void)threadloop {     while([[nsthread currentthread] iscancelled] == no)     {         [self.condition lock];         while(self.lock)         {             nslog(@"will wait");             [self.condition wait];              // "did wait" printed when have signaled condition change in sendnewevent method             nslog(@"did wait");         }          // read event event queue         ...           // lock condition again         self.lock = yes;         [self.condition unlock];     }  }  - (ibaction)sendnewevent:(id)sender {     [self.condition lock];     // put event in queue     ...       self.lock = no;     [self.condition signal];     [self.condition unlock]; } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -