GCD question

  • Hi,

    If I use this to initiate a background "thread":

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

    // do some stuff

    [self runMyFunction];

    [self runAnotherFunction];

    // do some more stuff

    });

    My question is with my sample calling runMyFunction or runAnotherFunction are they blocking?  Meaning will they block the background "thread" until they are complete and then it will continue?  Or must I put them into another kind of block so that they finish FIFO?  Thanks just looking for a confirmation as I'm moving to GCD from threads...

    rc
  • > If I use this to initiate a background "thread":
    >
    >
    > dispatch_async(dispatch_get_global_queue(0, 0), ^{
    >
    > // do some stuff
    >
    > [self runMyFunction];
    >
    > [self runAnotherFunction];
    >
    > // do some more stuff
    >
    > });
    >
    >
    > My question is with my sample calling runMyFunction or runAnotherFunction are they blocking?  Meaning will they block the background "thread" until they are complete and then it will continue?  Or must I put them into another kind of block so that they finish FIFO?  Thanks just looking for a confirmation as I'm moving to GCD from threads...

    You are correct in that they are "blocking" if I understand you correctly. There is nothing magic about the concept of GCD blocks, they are really just anonymous functions with a slightly unusual scope. You would be equally welcome to use the alternative dispatch_async_f API, which would have exactly the same outcome (code written in Mail; not complied!):

    void MyDispatchFunc(void *)
    {
    // do some stuff
    [self runMyFunction];
    [self runAnotherFunction];
    // do some more stuff
    }

    void foo(void)
    {
    dispatch_async_f(dispatch_get_global_queue(0, 0), MyDispatchFunc);
    }

    I hope it is obvious from this alternative formulation that there is nothing "magic" going on: the API call is just scheduling a call to MyDispatchFunc at the time GCD deems appropriate. Exactly the same is true of the example you gave, it is just that you are passing a block around instead of a function pointer.

    Hope that helps
    Jonny
  • On 19 Jul 2012, at 1:47 AM, Rick C. wrote:

    > If I use this to initiate a background "thread":
    >
    >
    > dispatch_async(dispatch_get_global_queue(0, 0), ^{
    >
    > // do some stuff
    >
    > [self runMyFunction];
    >
    > [self runAnotherFunction];
    >
    > // do some more stuff
    >
    > });
    >
    >
    > My question is with my sample calling runMyFunction or runAnotherFunction are they blocking? Meaning will they block the background "thread" until they are complete and then it will continue? Or must I put them into another kind of block so that they finish FIFO?  Thanks just looking for a confirmation as I'm moving to GCD from threads...

    Grand Central Dispatch and blocks are orthogonal; GCD uses blocks as its unit of work, but does nothing special to them internally. A block is an anonymous (Objective-) C function. It executes from top to bottom, and if it calls a function/method in the middle, the execution path goes into the function, and when the function returns, execution proceeds in the block from there — just like any other function. For that reason, you don't have to serialize -runMyFunction and -runAnotherFunction; they get run one after the other as in any other code.

    — F

    --
    Fritz Anderson -- Xcode 4 Unleashed: Now in stores! -- <http://x4u.manoverboard.org/>
  • Thanks all for the replies!

    rc

    On Jul 19, 2012, at 11:41 PM, Fritz Anderson wrote:

    > On 19 Jul 2012, at 1:47 AM, Rick C. wrote:
    >
    >> If I use this to initiate a background "thread":
    >>
    >>
    >> dispatch_async(dispatch_get_global_queue(0, 0), ^{
    >>
    >> // do some stuff
    >>
    >> [self runMyFunction];
    >>
    >> [self runAnotherFunction];
    >>
    >> // do some more stuff
    >>
    >> });
    >>
    >>
    >> My question is with my sample calling runMyFunction or runAnotherFunction are they blocking? Meaning will they block the background "thread" until they are complete and then it will continue? Or must I put them into another kind of block so that they finish FIFO?  Thanks just looking for a confirmation as I'm moving to GCD from threads...
    >
    > Grand Central Dispatch and blocks are orthogonal; GCD uses blocks as its unit of work, but does nothing special to them internally. A block is an anonymous (Objective-) C function. It executes from top to bottom, and if it calls a function/method in the middle, the execution path goes into the function, and when the function returns, execution proceeds in the block from there — just like any other function. For that reason, you don't have to serialize -runMyFunction and -runAnotherFunction; they get run one after the other as in any other code.
    >
    > — F
    >
    > --
    > Fritz Anderson -- Xcode 4 Unleashed: Now in stores! -- <http://x4u.manoverboard.org/>
    >
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today