문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

You can use GCD to schedule blocks for issuing on other threads. The two were introduced together, so any discussion of the one usually mentions the other. However, blocks are not in themselves inherently a multithreading mechanism.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top