0
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        while(!weakSelf.isAnotherThreadCompleted && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])
            ;
         [weakSelf doSomething];

    });

Is it correct to put async thread to wait another thread in this way?

taffarel
  • 4,015
  • 4
  • 33
  • 61

1 Answers1

1

You're going to stall the global queue corresponding to DISPATCH_QUEUE_PRIORITY_DEFAULT. If you dispatch something else to this queue, it won't execute until you're done waiting.

In addition, I don't think a run loop will exist in the thread corresponding to this dispatch queue, so you're going to create one by calling - currentRunLoop and since no source will be attached to it, it will return immediately with the value NO (or, there will be a random run loop, and the behavior will be completely unpredictable).

Why don't you just call a block at the end of your working thread? Or use only one mechanism (GCD, NSOperation, threads, run loops) and the synchronization that goes with it, instead of mixing them?

Guillaume
  • 4,331
  • 2
  • 28
  • 31
  • thanks for detailed answer, I just wanted to understand is it correct to do in this way or not. But i dont understand why currentRunLoop will return immediately ? right now it works as you said, thats why i open the topic. – taffarel Mar 21 '13 at 12:14
  • See the doc for `- runMode:beforeDate:`, if there is no input source or timer attached to the run loop, it will return immediately. In your case, you probably created a new run loop, so it has nothing attached to it. Also, when you use `dispatch_async`, you don't control on what thread your code is running, it's the OS and GCD's business. Run loops are thread specific, so you shouldn't use them in GCD calls (unless you're on the main queue, which is the only one guaranteed to be on a specific thread, the main thread). – Guillaume Mar 21 '13 at 12:23