Regarding IOS global queue, it is a good idea and we can use it to leverage multi-core CPU by sub-threads, but current now my question is how to manage these sub thread after it has been dispatched from queue? Seems Apple doesn’t provide such API.
For example, I create a serial queue, for the case one job is blocked dues to some reason then the whole queue will also be blocked. In fact we want to manage this job, current now seems no t method found.
Any discussion will be appreciated, thanks in advance.
Once a block is dispatched to a queue, it will execute. There is no API to prevent it.
You can
suspendthe queue, which will cause pending blocks to not executed until the queue is resumed. However, there is no way to stop blocks.The conventional method is to have a “check” in you code to see if the running block should stop early.
Note, that what you see as a “problem” is the exact definition of a serial queue. It only allows operations to process in the order in which they are submitted to the queue. You have to wait for the ones ahead of you to complete.
You can also check out
NSOperationQueuewhich provides an easier mechanism for canceling blocks that are waiting in the queue, but the only way to stop a block that is executing is for you to implement that mechanism in your block.If you find that you have to cancel lots of “blocking” blocks, then your design is most likely flawed, and you need to revisit your design.
Furthermore, if the tasks that are “behind” the one that is waiting really do not need to wait for that task, then why are you placing them behind it in a serial queue? Use a concurrent queue, or separate serial queues, or some other mechanism.
Now, let me state that you should really not use dispatch queue for blocking I/O operations. You can do it, and often it’s the easiest, but if you want the best performance characteristics for your app, you should use
dispatch_iowhich will integrate better with other CPU intensive threads.I guess the big takeaway here should be that instead of trying to get an answer to something like “How do I run a block on a serial queue when the block currently running is blocked?” you should be asking yourself how you can better design your application so that you do not have this scenario.
If the work does not have to be executed, then don’t wait for it. If the work has to be completed, they you just have to wait…