8

Possible Duplicate:
What is the difference between dispatch_get_global_queue and dispatch_queue_create?

I see some methods using gcd,but some of them may do it like this:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{

    dispatch_sync(dispatch_get_main_queue(), ^{

    });
});

but others may do it like this:

imageQueue_ = dispatch_queue_create("com.company.app.imageQueue", NULL);
dispatch_async(imageQueue_, ^{
        dispatch_async(dispatch_get_main_queue(), ^{

        });
    });

What's the differences?If i want to download many images from web,which is better?

Community
  • 1
  • 1
Jackie
  • 385
  • 1
  • 3
  • 10

2 Answers2

22

The best way to conceptualize queues is to first realize that at the very low-level, there are only two types of queues: serial and concurrent.

Serial queues are monogamous, but uncommitted. If you give a bunch of tasks to each serial queue, it will run them one at a time, using only one thread at a time. The uncommitted aspect is that serials queues may switch to a different thread between tasks. Serial queues always wait for a task to finish before going to the next one. Thus tasks are completed in FIFO order. You can make as many serial queues as you need.

The main queue is a special serial queue. Unlike other serial queues, which are uncommitted, in that they are "dating" many threads but only one at time, the main queue is "married" to the main thread and all tasks are performed on it. The main queue behaves nicely with the main thread's runloop so that small operations don't block the UI and other important bits. Like all serial queues, tasks are completed in FIFO order.

If serial queues are monogamous, concurrent queues are promiscuous. They will submit tasks to any available thread or even make new threads depending on system load. They may perform multiple tasks simultaneously on different threads. It's important that tasks submitted to the global queue are thread-safe and minimize side-effects. Tasks are submitted for execution in FIFO order, but order of completion is not guaranteed.

Bringing it back, all global queues are concurrent and all user queues are serial by default, unless define it as concurrent.

If your goal is to download images, you probably want a serial (user) queue. Downloading images is more of a bandwidth thing. You usually only want to do a one (or a few) at a time.

edit:blog post expanding on the above answer: http://amattn.com/2013/08/28/grand_central_dispatch_gcd_summary_syntax_best_practices.html

Community
  • 1
  • 1
amattn
  • 10,045
  • 1
  • 36
  • 33
  • From my experience, this answer is not accurate. User queue can be created (and function) as a concurrent queue. – Guy Oct 11 '14 at 13:21
  • Hi @amattn the link to your blog seems incorrect. By the way, this response is a very good summing up of GCD. – joan Jul 01 '22 at 12:49
3

If you use dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); GCD will automatically create enough threads for you to process the job, and use multiple cores if possible.

On the other hand, if you use imageQueue_ = dispatch_queue_create("com.company.app.imageQueue", NULL); you get a serial queue and a First In First Out behaviour.

If you are not worried about thread-safety or the order in which your jobs return, then use the first method.

Hesham
  • 5,294
  • 3
  • 34
  • 48
  • thanks for the great answer. So, say you're simply connecting to a server to get some json, using dataWithContentsOfURL:. Which would you use -- the first one? There's no concern about return order; and I guess there is no thread-safety concerns. So - simply use the global queue? {probably just with default priority} Cheers! – Fattie Sep 29 '14 at 06:09
  • 1
    "if you are not worried about thread-safety" - one should be always worried about thread-safety! – Maq May 16 '15 at 05:45