Create a high priority serial dispatch queue with GCD

妖精的绣舞 提交于 2019-11-27 02:08:27

问题


How can I create a custom serial queue that runs at high priority?

Right now I'm using myQueue = dispatch_queue_create("com.MyApp.MyQueue", NULL); but this doesn't seem to allow for setting a priority?


回答1:


Create a serial queue, then use dispatch_set_target_queue() to set its target queue to the high priority queue.

Here's how:

dispatch_set_target_queue(myQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));

Now myQueue should run serially with high priority. Here's another SO answer if you want to know more.




回答2:


The dispatch_queue_attr_make_with_qos_class function may be new since the accepted answer was posted, but something like:

dispatch_queue_attr_t priorityAttribute = dispatch_queue_attr_make_with_qos_class(
    DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, -1
);

myQueue = dispatch_queue_create("com.MyApp.MyQueue", priorityAttribute);

could give the queue a high priority ('quality of service'). There is a higher QOS class, but QOS_CLASS_USER_INITIATED is equivalent to DISPATCH_QUEUE_PRIORITY_HIGH.




回答3:


is it a requirement that you have a custom queue? If not, you could look at dispatching a block to the high priority global queue, which you can retrieve using:

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)

keep in mind that this is the global queue so it may impact other concurrent operations.



来源:https://stackoverflow.com/questions/17690740/create-a-high-priority-serial-dispatch-queue-with-gcd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!