dispatch_queue_set_specific和dispatch_get_specific

http://blog.csdn.net/mr_yong/article/details/50697656



dispatch_queue_set_specific就是向指定队列里面设置一个标识 如:

dispatch_queue_set_specific(queue1, queueKey1, &queueKey1,NULL);

就是向queue1对了中设置一个queueKey1标识。


dispatch_get_specific就是在当前队列中取出标识,注意iOS中线程和队列的关系,所有的动作都是在队列中执行的! 如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if (dispatch_get_specific(queueKey1)) {  
  2.     //说明当前的队列就是queue1  
  3. }else{  
  4.     </span>//说明当前的队列不是是queue1  
  5. }  


下面我写点代码说明这一点,:


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  main.m  
  3. //  gcd  
  4. //  
  5. //  Created by yongpengliang on 16/2/19.  
  6. //  Copyright © 2016年 jerry. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. int main(int argc, const charchar * argv[]) {  
  12.     @autoreleasepool {  
  13.         static voidvoid *queueKey1 = "queueKey1";  
  14.           
  15.         dispatch_queue_t queue1 = dispatch_queue_create(queueKey1, DISPATCH_QUEUE_SERIAL);  
  16.         dispatch_queue_set_specific(queue1, queueKey1, &queueKey1NULL);  
  17.           
  18.         NSLog(@"1. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  19.           
  20.         if (dispatch_get_specific(queueKey1)) {  
  21.             //当前队列是主队列,不是queue1队列,所以取不到queueKey1对应的值,故而不执行  
  22.             NSLog(@"2. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  23.             [NSThread sleepForTimeInterval:1];  
  24.         }else{  
  25.             NSLog(@"3. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  26.             [NSThread sleepForTimeInterval:1];  
  27.         }  
  28.           
  29.         dispatch_sync(queue1, ^{  
  30.             NSLog(@"4. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  31.             [NSThread sleepForTimeInterval:1];  
  32.               
  33.             if (dispatch_get_specific(queueKey1)) {  
  34.                  //当前队列是queue1队列,所以能取到queueKey1对应的值,故而执行  
  35.                 NSLog(@"5. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  36.                 [NSThread sleepForTimeInterval:1];  
  37.             }else{  
  38.                 NSLog(@"6. 当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  39.                 [NSThread sleepForTimeInterval:1];  
  40.             }  
  41.         });  
  42.         dispatch_async(queue1, ^{  
  43.             NSLog(@"7. t当前线程是: %@, 当前队列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());  
  44.             [NSThread sleepForTimeInterval:1];  
  45.         });  
  46.           
  47.         [NSThread sleepForTimeInterval:5];  
  48.     }  
  49.     return 0;  
  50. }  


输出结果:

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值