4

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.

7
  • 1
    I think you should think about NSLock and NSCondition. It is more proper way instead of infinite cycles.
    – malex
    Feb 14, 2014 at 9:16
  • 3
    I'm just curious. Why do you need such an inline solution? Feb 14, 2014 at 9:28
  • malex -- Hmm, I don't really see how NSLocking would help here. Maybe I'm missing something. Andrey - look at the first example imaginary code block. It would be incredibly easy / elegant to move items around, add items, and so on. Go ahead and write out a non-inline solution!
    – Fattie
    Feb 14, 2014 at 9:34
  • What are you waiting on?
    – jscs
    Feb 14, 2014 at 9:51
  • will sleep(1000000) meet your question ? Can waithere resume, if it can, when to resume?
    – KudoCC
    Feb 14, 2014 at 9:54

2 Answers 2

4

This is a solution using a semaphore, be careful not to introduce a deadlock - you need some way of telling your application something has finished, you can either do that using the NSNotificationCentre like you suggested but using a block is much easier.

[self someOtherStuffWithCompletion:nil];

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[self someOtherStuffWithCompletion:^{
  dispatch_semaphore_signal(semaphore);
}];

NSLog(@"waiting");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"finished");

[self someOtherStuffWithCompletion:nil];
9
  • Oliver, thank you so much ... ahhh. Well now ... this entirely happens on the "same" thread, is that correct? (ie: you don't have to send someOtherStuffWithCompletion to another thread, or anything like that?) thanks!
    – Fattie
    Feb 15, 2014 at 9:35
  • TBC, when you say avoid a deadlock, do you mean: "don't forget to" eventually call dispatch_semaphore_signal? Or something more hideous I don't understand? :) Thanks!
    – Fattie
    Feb 15, 2014 at 9:37
  • 1
    someOtherStuffWithCompletion needs to happen on another thread you are correct Feb 16, 2014 at 16:08
  • Ah, another thread, I see -- crap! I'm looking for a solution just on the same thread. Damn I have to make the question clearer, sorry! Still incredibly useful though ........ thanks again
    – Fattie
    Feb 17, 2014 at 7:54
  • If you are doing the work on the same thread then you dont need to wait, the next operation will only execute once the previous one has finished - thats the way computers work. Feb 17, 2014 at 9:42
1

I would suggest to use a NSOperationQueue and to wait for all tasks until they are finished at specific points. Something like that:

self.queue = [[NSOperationQueue alloc] init];

// Ensure a single thread
self.queue.maxConcurrentOperationCount = 1;

// Add the first bunch of methods
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method1) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method2) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method3) object:nil]];

// Wait here
[self.queue waitUntilAllOperationsAreFinished];

// Add next methods
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method4) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method5) object:nil]];

// Wait here
[self.queue waitUntilAllOperationsAreFinished];

HTH

6
  • Great thinking -- but those guys would all be on different threads right?
    – Fattie
    Feb 14, 2014 at 9:42
  • 1
    Not if you set maxConcurrentOperationCount to 1.
    – Marcio
    Feb 14, 2014 at 9:50
  • 1
    Marcio is totally right. I updated the code example above to reflect this behaviour.
    – anka
    Feb 14, 2014 at 10:14
  • Hmm guys, I'm wondering, if you set .maxConcurrentOperationCount = 1, does that put all of them on (1) NEW (different) thread? or does it keep all of them on "our" same thread? Cheers....
    – Fattie
    Feb 14, 2014 at 17:21
  • 1
    In one new different thread. But I think you want to have it all in the same thread. Or maybe on the main thread?
    – anka
    Feb 14, 2014 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.