Jason Renaldo
Jason Renaldo

Reputation: 2822

Will blocks hold execution on the thread it is run on?

If I had a block like so:

(void) ^contrivedExample = ^{//some expensive operation//};

And use it like so:

int test = 1;

contrivedExample()

test++;

Since incrementing test takes no time, will this only occur after my block has fully executed?

Upvotes: 2

Views: 76

Answers (3)

Alex311
Alex311

Reputation: 378

Yes, they will hold execution. The increment will occur only after your expensive block finishes executing.

If you need to brush up on your blocks, here's a good read on the matter: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

Upvotes: 2

Guy Kogus
Guy Kogus

Reputation: 7341

Yes. You can even have it run on a different thread by calling either dispatch_sync or dispatch_async.

dispatch_sync means the block runs on a different thread, while the current thread waits for the block to complete.

dispatch_async sends the block to another thread and the current thread continues.

Upvotes: 2

Jonathan Arbogast
Jonathan Arbogast

Reputation: 9650

Yes. Incrementing test will occur after the block has completely finished executing. Blocks aren't asynchronous in and of themselves, although they are often used by APIs that are asynchronous.

Upvotes: 2

Related Questions