[ GCD ]dispatch_async() question

Discussion in 'Mac Programming' started by Sergio10, Jul 19, 2011.

  1. Sergio10 macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    What does it mean the following code?

    I've reade about cuncurrency but still don't understand:
    1. Why do we need autorelease pool?
    2. I assume logInfo method will be executed in other thread.(without hanging main thread) Correct?
    3. What difference between thread and queue?

    Thank you, in advance.
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    1) GCD is "below" Cocoa in the stack of components that make up the OS, so it has no knowledge of autorelease pools and the like.
    2) No. Note the dispatch_get_main_queue(). The main queue executes on the main thread. This code is likely *already on* a background thread/queue and needs to get something back to the main thread. If it were dispatching to a local queue, or one of the global concurrent queues, then it would not block the main thread.
    3) A dispatch queue can be thought of as the smallest thing necessary to ensure that a group of units of work execute in a particular order. When it has work to do, it will grab a thread (from a pool of threads if a local or global queue, or the main thread if the main queue), and do the work on that thread. One advantage of this is that the system can avoid spawning tons of unnecessary threads in some situations.
     
  3. admanimal, Jul 19, 2011
    Last edited: Jul 19, 2011

    admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    Technically dispatch queues have their own autorelease pools, so creating another one on top of that is not mandatory. However, pools in dispatch queues may not be drained as often as the main pool, so it can be a good idea to create and release a pool manually instead.
     

Share This Page