Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between global queues and user queues? [duplicate]

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?

like image 355
Jackie Avatar asked Dec 16 '12 06:12

Jackie


People also ask

What is the difference between global and main queue?

The global queues are concurrent queues and from the main page for dispatch_get_global_queue: Unlike the main queue or queues allocated with dispatch_queue_create (), the global concurrent queues schedule blocks as soon as threads become available ("non-FIFO" completion order). The global concurrent queues represent three priority bands:

What is the difference between queue and topic in Java?

What is the Difference Between Queue and Topic. The main difference between queue and topic is that queue is the message-oriented middleware used in point to point message domain, while the topic is the message-oriented middleware used in publisher- subscriber message domain. Java Message Service (JMS) is an application program interface ( API) ...

What is the difference between a flag and a queue?

The flag is real at the same time as a message has been located in the mailbox and cleared at the same time as the message is removed. A queue in standard has very particular that means in computing as a box facts shape with first-in-first-out (FIFO) get right of entry to semantics.

What are the different types of queue in GCD?

GCD provides three main types of queues: Main queue: runs on the main thread and is a serial queue. This is a common choice to update the UI after completing work in a task on a concurrent queue.


1 Answers

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

like image 184
amattn Avatar answered Sep 20 '22 15:09

amattn