1

I read the "IOS Developer Library" about the "Run Loops" theme, in the article, one sentence says "It is possible to run a run loop recursively". My question is in which scenario should use the recursive run loop please?

My another question is about the statement "The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.". How can a run loop sleep, can the main thread's run loop sleep when no event comes? What about the second thread's situation?

one example of the nested run loop that I found from Internet is that like below:

 [NSThread detachNewThreadSelector:@selector(runOnNewThread) toTarget:self withObject:nil]; 
 while (!end) {
    NSLog(@”runloop…”);
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    NSLog(@”runloop end.”);
 }

The current thread will be blocked until the work in another thread has finished. But why this happen, how the current thread can be blocked?
Can anyone answer my question?

Jay
  • 113
  • 1
  • 11
  • Have you ever had occasion to run a run loop explicitly at all? – matt Sep 04 '15 at 02:43
  • just speculating here, but i think they just mean you can run a run loop from within a run loop with no side effects, since the run loops get added to a queue when they are made and run at a later stage – Fonix Sep 04 '15 at 02:50
  • @matt, I just want to learn it now, I did not meet any case to use the run loop explicitly in practical situation. – Jay Sep 04 '15 at 03:32
  • @Fonix, can you explain me that " run loops get added to a queue when they are made and run at a later stage", what does this mean? – Jay Sep 04 '15 at 03:33
  • 2
    And you probably never will. So why are you asking about this? – matt Sep 04 '15 at 03:34
  • @matt, OK let's leave it temporarily, how to understand this question "The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none" please? – Jay Sep 04 '15 at 03:41
  • Look, Jay, you're never in your life going to make an NSThread and give it a run loop. That stuff is way in the past. If you use threads at all you'll use GCD or NSOperation. So let's just get off it, okay? – matt Sep 04 '15 at 04:09
  • i would just look at [the docs](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html) for an explanation on what a run loop is and how it works – Fonix Sep 04 '15 at 04:24
  • see what i did there? i made this question recursive :P edit: ill actually put this as an answer – Fonix Sep 04 '15 at 04:38

1 Answers1

0

maybe an analogy is, pretend a run loop is an infinite while loop (which it basically is), then if you made another infinite while loop inside that one, the outer while loop would never loop again because the inner one is infinite, so what they have done is made it so when you make an inifinite while loop in another, it instead goes out of the outer one, and runs along beside it (on another thread)

in response to your update:

surely it will be blocked for no other reason than the while loop doesnt have an end condition

while (!end) {

if end is controlled from another thread, then that thread would have to set end to true before the current thread can move on from this while loop.

this code just seems really bad, it keep restarting the current runloop, probably defeating it purpose

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • what are you trying to do? you really should probably be looking at [Grand Central Dispatch](https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/) if you are trying to do anything threading related. The only reason for using a run loop i think is maybe to use OpenGL – Fonix Sep 04 '15 at 07:07
  • Good explanation, what if we remove the code `[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];` then? It is still a inifinite loop.What will happen then? – Jay Sep 04 '15 at 09:08
  • yep, loop will keep going until `end` is changed, blocking the entire thread (because it cant finish executing the while loop). there is a term for this kind of thing, its called a busy wait, and is really bad practise, should use proper threading mechanisms like thread locks and semaphores which GCD does – Fonix Sep 04 '15 at 09:10
  • May I know why the code stop at `[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];`. But when I click the other button, the UI has response and is not frozen, and the while loop begins to run? – Jay Sep 06 '15 at 18:35