我有来自API时代的现有代码。这是一个基于MPCreateTask的线程。看起来我可以将其移到GCG队列中,但是有点麻烦。当前基于MPCreateQueue的三个队列用于三个优先级。
在GCD中,我已经弄清楚了,并测试了以下代码作为GCD重构的概念证明(天哪,我讨厌这个词,但很合适)。
首先,这是否可以实现我所期望的,所有动作(输入到例程的块)将是串行的。动作将具有由例程调度它们指定的优先级。
第二,是否有更好的方法可以做到这一点?
// set up three serial queues
dispatch_queue_t queueA = dispatch_queue_create("app.queue.A" , DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queueB = dispatch_queue_create("app.queue.B" , DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queueC = dispatch_queue_create("app.queue.C" , DISPATCH_QUEUE_SERIAL);
// set the target queues so that all blocks posted to any of the queues are serial
// ( the priority of the queues will be set by queueC.
dispatch_set_target_queue( queueB, queueC ) ;
dispatch_set_target_queue( queueA, queueB ) ;
void lowPriorityDispatch( dispatch_block_t lowAction )
{
dispatch_async( queueC, ^{
lowAction() ;
}) ; …Run Code Online (Sandbox Code Playgroud)