Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

blocks and threads

I want to know if blocks in c / cocoa run on a seperate thread to the main thread. Would they be useful for executing computationally expensive code while leaving the UI responsive?

like image 350
dubbeat Avatar asked Dec 07 '22 00:12

dubbeat


2 Answers

Blocks are just snippets of code bundled up into a callable object. How they run is entirely up to the code that calls it.

Running blocks on a separate thread is not only possible, but is precisely the reason the blocks concept was introduced. It exists to support Grand Central Dispatch, which hides a lot of the complexity of concurrent programming behind a task-oriented model.

like image 145
Marcelo Cantos Avatar answered Dec 21 '22 23:12

Marcelo Cantos


They don't have to run on another thread, but they can. You can schedule them on NSOperationQueues or GCD queues, and those queues can be drained by background threads.

And yes, this can be a useful construct to help you get time consuming work off the main thread. But that's not all that blocks are useful for, and conversely you can do background processing with or without blocks.

like image 28
Firoze Lafeer Avatar answered Dec 21 '22 23:12

Firoze Lafeer