Consider this code:
[self otherStuff];
// "wait here..." until something finishes
while(!self.someFlag){}
[self moreStuff];
Note that this all happens ON THE SAME THREAD - we do not want to go to another thread.
otherStuff could do things like connect to the cloud, get input from the user, etc. so it would take a lot of time and could follow many possible paths.
otherStuff would set self.someFlag to true, when otherStuff is finally finished.
This works perfectly and there's no problem with it at all -- except that it's lame to burn up the processor like that with the empty loop!!
Quite simply, is there a way to say something like ..
halt here, until (some message, interrupt, flag, boolean, whatever?)
Rather than just while(!self.someFlag){}
(Note the alternative is to "chain" the procedures ... so at the end of "otherStuff", you and all the other programmers have to "just know" that you have to next call "moreStuff", regardless of how otherStuff plays out, etc. Of course, that is very messy when you have to add new procedures or change the order of things.) Cheers!!
BTW there are already two excellent answers below regarding the situation when you want DIFFERENT THREADS.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…