GCD and queues

Discussion in 'iOS Programming' started by dude1234, Mar 13, 2013.

  1. dude1234, Mar 13, 2013
    Last edited: Mar 13, 2013

    dude1234 macrumors newbie

    Feb 17, 2013
    i cant find any doc that explaine how does the whole idea of queues work. i mean, i understand that firing tasks on background threads can be very useful, but what about the timing? how does an api like gcd handles it?
    what happens if i run a method and inside that method i run a block in another thread, what is going to happen if the block runs slower than the method? what if that mathod needs the block to finish its calc to continue executing??

    i`ve heard GCD handles it, but i have to understand how! does it know how much time every task is going to take before i even run it?!


    if you i`ll be willing to accent any means of GCD and Queues visualizationso i can grasp the theory behind it, tnx.
  2. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia

    Queues hold jobs that are waiting to be executed, and execute them based on the way the queue is set up. A GCD can run on the main thread or on a background thread.

    If it runs on the main thread, then other execution on the main thread stops while a job is run.

    If it runs on a background thread, how it is run depends on the hardware platform. If you are running on a multi-core device (iPhone 4s and later, iPad 2 and later, and the recent iPod touch models) then the jobs on the background queue run at the exact same time as your code on the main thread. There is no "knowing how long a job will take." It runs until it's done., and you as the programmer have to take steps to make sure code running on different threads does not interfere.

    GCD includes facilities that let you send notifications when operations or groups of operations are completed, and for synchronizing work. It's up to you to use those facilities to coordinate.

    Concurrent programming is a complex, difficult subject. I suggest you read the Grand Central Dispatch (GCD) Reference in Xcode. However, if you're not a fairly advanced developer then the concepts and terms might be over your head. (Thread, semaphores, locks, barriers, etc)

Share This Page