title: Grand Central Dispatch vs. NSThread
body: <p>I created some test code for NSThread and Grand Central Dispatch (GCD):</p>
  
  <pre><code>- (void)doIt:(NSNumber *)i
  {
   sleep(1);
   NSLog(@"Thread#%i", [i intValue]);
  }
  
  - (IBAction)doWork:(id)sender
  {
  
   for (int i = 0; 10 &gt; i; i++) {
      NSNumber *t = [NSNumber numberWithInt:i];
      [NSThread detachNewThreadSelector:@selector(doIt:) toTarget:self withObject:t];
   }
  
   sleep(1);
  
   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   dispatch_apply(10, queue, ^(size_t i) {
      sleep(1);
      NSLog(@"GCD#%u",(int)i);
   });
  }
  </code></pre>
  
  <p>And the results:</p>
  
  <pre><code>2011-04-13 19:41:07.806 GDC[1494:5e03] Thread#0
  2011-04-13 19:41:07.813 GDC[1494:6903] Thread#3
  2011-04-13 19:41:07.812 GDC[1494:6403] Thread#2
  2011-04-13 19:41:07.812 GDC[1494:5f03] Thread#1
  2011-04-13 19:41:07.813 GDC[1494:6e03] Thread#4
  2011-04-13 19:41:07.814 GDC[1494:7303] Thread#5
  2011-04-13 19:41:07.814 GDC[1494:7803] Thread#6
  2011-04-13 19:41:07.815 GDC[1494:7d03] Thread#7
  2011-04-13 19:41:07.815 GDC[1494:8203] Thread#8
  2011-04-13 19:41:07.816 GDC[1494:8703] Thread#9
  2011-04-13 19:41:08.812 GDC[1494:707] GCD#0
  2011-04-13 19:41:09.816 GDC[1494:707] GCD#1
  2011-04-13 19:41:10.819 GDC[1494:707] GCD#2
  2011-04-13 19:41:11.825 GDC[1494:707] GCD#3
  2011-04-13 19:41:12.828 GDC[1494:707] GCD#4
  2011-04-13 19:41:13.833 GDC[1494:707] GCD#5
  2011-04-13 19:41:14.838 GDC[1494:707] GCD#6
  2011-04-13 19:41:15.848 GDC[1494:707] GCD#7
  2011-04-13 19:41:16.853 GDC[1494:707] GCD#8
  2011-04-13 19:41:17.857 GDC[1494:707] GCD#9
  </code></pre>
  
  <p>NSThreads work as I expected: The tasks run concurrently and each thread sleeps for 1 second.</p>
  
  <p>The dispatch_apply does not work as I expected: Why is the order sequential? Why does each loop wait until the previous loop finishes? </p>
  
  <p>Thanks for the help.</p>