Is there any way to “wait here…” in code - just like an empty loop?

早过忘川 提交于 2019-11-28 01:30:18

问题


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.


回答1:


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];



回答2:


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



来源:https://stackoverflow.com/questions/21775023/is-there-any-way-to-wait-here-in-code-just-like-an-empty-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!