前面我们说了block中提到它用于多线程,而gcd则是其用于多线程的典型。gcd其全称(Grand Central Dispatch)
那到底什么叫gcd,官方的解释如下:
Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and OS X
翻译:
Grand Central Dispatch(GCD)包括语言的特点,运行库,和系统的完善,提供系统的,全面的改进支持并行执行代码在多核硬件在iOS和OS X。
ios里主要用到其的就是关于队列的管理和调度,说到队列这里有三种队列
GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Blocks submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes. GCD offers three kinds of queues:
-
Main: tasks execute serially on your application’s main thread
-
Concurrent: tasks are dequeued in FIFO order, but run concurrently and can finish in any order.
-
Serial: tasks execute one at a time in FIFO order
The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:
-
Calling
dispatch_main
-
Calling
UIApplicationMain
(iOS) orNSApplicationMain
(OS X) -
Using a
CFRunLoopRef
on the main thread
Use concurrent queues to execute large numbers of tasks concurrently. GCD automatically creates three concurrent dispatch queues that are global to your application and are differentiated only by their priority level. Your application requests these queues using the dispatch_get_global_queue
function. Because these concurrent queues are global to your application, you do not need to retain and release them; retain and release calls for them are ignored. In OS X v10.7 and later, you can also create additional concurrent queues for use in your own code modules.
Use serial queues to ensure that tasks to execute in a predictable order. It’s a good practice to identify a specific purpose for each serial queue, such as protecting a resource or synchronizing key processes. Your application must explicitly create and manage serial queues. It can create as many of them as necessary, but should avoid using them instead of concurrent queues just to execute many tasks simultaneously.
Important: GCD is a C level API; it does not catch exceptions generated by higher level languages. Your application must catch all exceptions before returning from a block submitted to a dispatch queue.
dispatch_async
dispatch_async_f
dispatch_sync
dispatch_sync_f
dispatch_after
dispatch_after_f
dispatch_apply
dispatch_apply_f
dispatch_once
翻译过来大概是说
GCD提供和管理FIFO(First In First Out)队列,您的应用程序可以在块对象的形式提交的任务。块调度队列在系统管理的线程池中执行。不能确保在哪个线程上执行任务。GCD提供三种队列
Main(全局的可用的串行队列):在主线程中串行执行任务
Concurrent(并发队列):队列按照先进先出的顺序,但同时运行,可以以任何顺序完成
Serial(串行队列不过是私有):在任意时候以先进先出的顺序执行
主要队列是自动创建的系统和应用程序的主线程关联。你的应用程序使用一个(只有一个)的调用提交的主要队列三块的方法:
先呼叫dispatch_main,也就是从子线程跳出到主线程再呼叫main.h中的UIApplicationMain
最后调用主线程中的CFRunLoopRef
使用并行队列执行大量的任务的同时。GCD自动创建三个并发调度队列是全局应用程序,只有它们的优先级区分。您的应用程序请求这些队列使用dispatch_get_global_queue功能。因为这些并行的队列是覆盖到您的应用程序,您不需要保留和释放;retain和release,他们被忽略。在OS X v10.7或以后,你还可以创建您自己的代码模块使用额外的并发队列
使用串行队列来确保任务按一定顺序执行。这是一个很好的实践来确定每个串行队列特定目的,如保护资源或同步的关键过程。您的应用程序必须显式地创建和管理串行队列。它可以创建必要的许多人,但应避免使用它们而不是并发队列只执行许多任务同时。
GCD是一个C级API;它不抓捕的更高水平的语言产生异常。你的应用程序必须在提交给调度队列块之前返回所有异常。
dispatch_async
dispatch_async_f比较,它们之音的区别就是多了一个_f,再就是用_f多了一个参数,其它的好像没什么区别,官网上也没查到
//这里用的是串行队列
dispatch_queue_t que=dispatch_queue_create("123",NULL);
//异步执行私有调度队列
dispatch_async(que, ^{
//这里是当上面的内容执行完马上执行,即同步执行
dispatch_sync(dispatch_get_main_queue(), ^{
});
//这里不确定什么时候能执行即异步中的异步
// dispatch_async(dispatch_get_main_queue(), ^{
//
// });
});
//释放
dispatch_release(que);
//这里是异步执行并发行队列
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//同步执行全局可用的串行队列
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
上面是不是处处都包含着block