转自:http://yxiaogang.blog.163.com/blog/static/196021162201110255429370/ 尊重原创,对其中的错别字以及部分发现的差错进行修改,并对内容进行补充。
我们几乎可以调度队列去完成所有用线程来完成的任务。调度队列相对于线程代码更简单,易于使用,更高效。
Dispatch Queues
Dispatch Queues从使用的角度将更象另一种形式的Operation Queues只是 Operation Queues是用Object C的Dispatch Queues是C的
dispatch Queues有serial Queues 也被称为私有dispatch Queues,一个时间只能运行一个task,顺序运行
dispatch_queue_t queue;
queue = dispatch_queue_create("myQueue", NULL);
dispatch_async(queue, ^{
printf("Do some work here.\n");
});
printf("The first block may or may not have run.\n");
dispatch_sync(queue, ^{
printf("Do some more work here.\n");
});
printf("Both blocks have completed.\n");
这里使用了同步dispatch和异步dispatch,推荐使用dispatch_async这样才能真正体现其中的优势同步相当于WaitUntilDone = YES
还有一种就是Concurrent Queues每个程序系统自动提供了3个Concurrent Queues
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t aHQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t aLQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
啥意思一看就明白,3个优先级别的concurrent queues
最后一个特殊的Dispatch Queue就是main dispatch Queue 也是程序启动自动生成
dispatch_queue_t mainQueue = dispatch_get_main_queue();
concurrent queues和main queue 都是由系统生成而且 dispatch_suspend, dispatch_resume, dispatch_set_context,这些函数对他们无效
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t queue; |
queue = dispatch_queue_create("com.example.MyQueue", NULL); |
the data stored in the queue’s context pointer. (The
myInitializeDataContextFunction
myCleanUpDataContextFunction
referenced from the code are custom functions that you would provide to initialize and clean up the contents of the data structure itself.) The context pointer passed to the finalizer function contains the data object associated with the queue.
void myFinalizerFunction(void *context) |
{ |
MyDataContext* theData = (MyDataContext*)context; |
|
// Clean up the contents of the structure |
myCleanUpDataContextFunc |
|
// Now release the structure itself. |
free(theData); |
} |
|
dispatch_queue_t createMyQueue() |
{ |
MyDataContext* data = (MyDataContext*) malloc(sizeof(MyDataContext)); |
myInitializeDataContextF |
|
// Create the queue and set the context data. |
dispatch_queue_t serialQueue = dispatch_queue_create("com.example.CriticalTaskQueue", NULL); |
if (serialQueue) |
{ |
dispatch_set_context(serialQueue, data); |
dispatch_set_finalizer_f(serialQueue, &myFinalizerFunction); |
} |
|
return serialQueue; |
} |
dispatch_queue_t myCustomQueue; |
myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL); |
|
dispatch_async(myCustomQueue, ^{ |
printf("Do some work here.\n"); |
}); |
|
printf("The first block may or may not have run.\n"); |
|
dispatch_sync(myCustomQueue, ^{ |
printf("Do some more work here.\n"); |
}); |
printf("Both blocks have completed.\n"); |
void average_async(int *data, size_t len, |
dispatch_queue_t queue, void (^block)(int)) |
{ |
// Retain the queue provided by the user to make |
// sure it does not disappear before the completion |
// block can be called. |
dispatch_retain(queue); |
|
// Do the work on the default concurrent queue and then |
// call the user-provided block with the results. |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
int avg = average(data, len); |
dispatch_async(queue, ^{ block(avg);}); |
|
// Release the user-provided queue when done |
dispatch_release(queue); |
}); |
} |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
|
dispatch_apply(count, queue, ^(size_t i) { |
printf("%u\n",i); |
}); |