multithreading - In Objective-c how do I ensure that one code is called after another code? -
i don't mean synchronize , stuff. not in general programming practice actually. think there should done.
for example, have thread suspend self , wait till event occur. event occur before thread suspend itself.
so want event wait till thread suspend before occurs.
how do that?
for example, want 5 things on mainqueue.
do something. wait till it's finished, next.
i then:
-(void) vdosomestuffsinsequence:(nsarray *) arblarrayofblockstoexecuteonmainthread { if (self.blockthis) { po(@"cancel push view controller"); return; } @synchronized(self) { self.blockthis=true; } assertmainthread nsmutablearray * arblnotfirstblocks = [arblarrayofblockstoexecuteonmainthread mutablecopy]; void(^blocktoexecute)() = arblnotfirstblocks[0];//execute first block right away blocktoexecute(); po(@"execute pop push etc"); [arblnotfirstblocks removeobjectatindex:0]; [[nsoperationqueue new] addoperationwithblock:^{ po(@"before first suspend"); [self vsuspendandhaltthisthreadtillunsuspended]; //this happen after (instead of before) code execute [self vcontinue called] po(@"after first suspend"); (void(^blocktoexecute)() in arblnotfirstblocks) { while(false); [[nsoperationqueue mainqueue] addoperationwithblock:^{ blocktoexecute();//dothisfirst must unsuspend thread somewhere }]; [self vsuspendandhaltthisthreadtillunsuspended]; } [[nsoperationqueue mainqueue]addoperationwithblock:^{ @synchronized(self) { self.blockthis=false; } [self vcontinue]; }]; [self vsuspendandhaltthisthreadtillunsuspended]; po(@"finish vdosomestuffsinsequence"); }]; }
polling, waiting, , long or indefinite thread or resource blocking best avoided. nevertheless... can wait using condition variables, dispatch_group_wait
, or -[nsoperationqueue waituntilalloperationsarefinished]
.
Comments
Post a Comment