1

如果我像这样存储 dispatch_queue_t :

@property(assign, nonatomic) dispatch_queue_t myQueue;

...

_myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

后来,当我做这样的操作时

dispatch_async(_myQueue, ^{
  NSLog(@"Hi!");
});

然后在别的地方

dispatch_async(_myQueue, ^{
  NSLog(@"Hello!");
});

这些块是否在同一个线程上执行?如果不是,我如何确保它们是?基本上我想保留对线程的引用并使其仅在该线程上执行一些操作。

4

1 回答 1

5

How threads are assigned to queues is an implementation detail of Grand Central Dispatch. Two blocks dispatched to a (serial or concurrent) queue are not necessarily executed on the same thread. The only exception is the "main queue" which only executes on the main thread.

If you really have the requirement that the code executes on the same thread, you have to use a different threading method, e.g. NSThread or pthread_create.

于 2013-09-01T19:33:28.353 回答