第一部分:线程与队列
线程是代码执行的路径,队列则是用于保存以及管理任务的,线程负责去队列中取任务进行执行。
例如:在主线程调用如下代码
- dispatch_sync(queue, ^{
- task()
- });
- -(void)task
- {
- NSLog(@"1. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- }
- dispatch_async(queue, ^{
- task()
- });
以上粗浅解释了IOS中的线程与队列
第二部分:GCD获取当前队列的方法以及它为何在ios6中被丢弃
- void func(dispatch_queue_t queue, dispatch_block_t block)
- {
- if (dispatch_get_current_queue() == queue) {
- block();
- }else{
- dispatch_sync(queue, block);
- }
- }
- - (void)deadLockFunc
- {
- dispatch_queue_t queueA = dispatch_queue_create("com.yiyaaixuexi.queueA", NULL);
- dispatch_queue_t queueB = dispatch_queue_create("com.yiyaaixuexi.queueB", NULL);
- dispatch_sync(queueA, ^{
- dispatch_sync(queueB, ^{
- dispatch_block_t block = ^{
- //do something
- };
- func(queueA, block);
- });
- });
- }
那么应该如何保证GCD方法可重入呢?
- dispatch_queue_set_specific 标记队列
- if (dispatch_get_specific(queueKey1)) {
- //说明当前的队列就是queue1
- }else{
- </span>//说明当前的队列不是是queue1
- }
- #import <Foundation/Foundation.h>
- int main(int argc, const charchar * argv[]) {
- @autoreleasepool {
- static voidvoid *queueKey1 = "queueKey1";
- dispatch_queue_t queue1 = dispatch_queue_create(queueKey1, DISPATCH_QUEUE_SERIAL);
- dispatch_queue_set_specific(queue1, queueKey1, &queueKey1, NULL);
- NSLog(@"1. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- if (dispatch_get_specific(queueKey1)) {
- //当前队列是主队列,不是queue1队列,所以取不到queueKey1对应的值,故而不执行
- NSLog(@"2. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }else{
- NSLog(@"3. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }
- dispatch_sync(queue1, ^{
- NSLog(@"4. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- if (dispatch_get_specific(queueKey1)) {
- //当前队列是queue1队列,所以能取到queueKey1对应的值,故而执行
- NSLog(@"5. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }else{
- NSLog(@"6. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }
- });
- dispatch_async(queue1, ^{
- NSLog(@"7. t当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- });
- [NSThread sleepForTimeInterval:5];
- }
- return 0;
- }
task函数这个任务将会提交到队列queue上,主线程负责去执行这个任务。
dispatch_get_current_queue这个方法可以很方便的获取到当前线程,可为什么要丢弃这个函数呢?请看一下代码
为了防止类似的误用,苹果在iOS6废弃了dispatch_get_current_queue()方法。强大的dispatch_get_current_queue()也只能当作一个调试工具了。
dispatch_queue_set_specific就是向指定队列里面设置一个标识 如:
dispatch_queue_set_specific(queue1, queueKey1, &queueKey1,NULL);
就是向queue1对了中设置一个queueKey1标识。
输出结果:
2016-02-19 14:31:23.390 gcd[96865:820267] 1.当前线程是: <NSThread: 0x1001053e0>{number = 1, name = main},当前队列是: <OS_dispatch_queue: com.apple.main-thread[0x100059ac0]>。
2016-02-19 14:31:23.391 gcd[96865:820267] 3.当前线程是: <NSThread: 0x1001053e0>{number = 1, name = main},当前队列是: <OS_dispatch_queue: com.apple.main-thread[0x100059ac0]>。
2016-02-19 14:31:24.396 gcd[96865:820267] 4.当前线程是: <NSThread: 0x1001053e0>{number = 1, name = main},当前队列是: <OS_dispatch_queue: queueKey1[0x103000000]>。
2016-02-19 14:31:25.400 gcd[96865:820267] 5.当前线程是: <NSThread: 0x1001053e0>{number = 1, name = main},当前队列是: <OS_dispatch_queue: queueKey1[0x103000000]>。
2016-02-19 14:31:26.402 gcd[96865:820367] 7. t当前线程是: <NSThread: 0x100105e10>{number = 2, name = (null)},当前队列是: <OS_dispatch_queue: queueKey1[0x103000000]>。
Program ended with exit code: 0