Re: static acting like __block in GCD singleton pattern
Re: static acting like __block in GCD singleton pattern
- Subject: Re: static acting like __block in GCD singleton pattern
- From: Kyle Sluder <email@hidden>
- Date: Sat, 29 Jun 2013 10:00:30 -0700
On Sat, Jun 29, 2013, at 09:53 AM, Kyle Sluder wrote:
> Yes. In order to make it possible to capture local variables in a block,
> they are potentially hoisted to a storage that has the same lifetime as
> the block. For readonly accesses, that doesn't matter; the value of the
> expression is captured into the block context at the time of block
> execution.
Urp, breaking my own explanation here. I meant to say "captured into the
block context at the time of block context construction."
Here's a quick demo program that shows the difference between what the
block actually captures:
// t.c
#include <stdio.h>
int main(int argc, char **argv)
{
int a = 7;
__block int b = 10;
void (^f)(void) = ^{ printf("a is %d\nb is %d\n", a, b); };
a = 20;
b = 42;
f();
return 0;
}
// end t.c
If you run this program, you get the following output:
a is 7
b is 42
In the first case, the block context captured the _value_ of a. In the
second, it captured the _pointer to b's movable storage_.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden