Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4. @property (strong, nonatomic) dispatch_queue_t queue;
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad {
  10.     [super viewDidLoad];
  11.    
  12.     _queue =  dispatch_queue_create("queue", NULL);
  13.  
  14.    // [self withBarrier];
  15.    // [self withDispatchGroup];
  16.    // [self withSemaphore];
  17.       [self withCondition];
  18.    // [self withOperation];
  19.    
  20. }
  21.  
  22.  
  23. - (id)withBarrier {
  24.  
  25.     dispatch_async(_queue, ^{
  26.  
  27.         [NSThread sleepForTimeInterval:2];
  28.          NSLog(@"async block");
  29.        
  30.     });
  31.    
  32.      dispatch_barrier_sync(_queue, ^{
  33.          NSLog(@"barrier");
  34.      });
  35.    
  36.     NSLog(@"return value");
  37.     return nil;
  38. }
  39.  
  40.  
  41. - (id)withDispatchGroup {
  42.  
  43.     dispatch_group_t group = dispatch_group_create();
  44.    
  45.     dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
  46.         [NSThread sleepForTimeInterval:2];
  47.         NSLog(@"async block");
  48.     });
  49.    
  50.     dispatch_group_wait(group,DISPATCH_TIME_FOREVER);
  51.    
  52.     NSLog(@"return value");
  53.    
  54.     return nil;
  55. }
  56.  
  57.  
  58. - (id)withSemaphore {
  59.    
  60.     dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  61.    
  62.     dispatch_async(_queue, ^{
  63.      
  64.         [NSThread sleepForTimeInterval:2];
  65.         NSLog(@"async block");
  66.         dispatch_semaphore_signal(semaphore);
  67.     });
  68.    
  69.     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  70.     NSLog(@"return value");
  71.     return nil;
  72. }
  73.  
  74.  
  75.  
  76. - (id)withCondition {
  77.    
  78.     NSCondition * locker = [[NSCondition alloc]init];
  79.     [locker lock];
  80.    
  81.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  82.         [NSThread sleepForTimeInterval:2];
  83.         NSLog(@"completion block");
  84.         [locker signal];
  85.     });
  86.    
  87.     [locker wait];
  88.     [locker unlock];
  89.    
  90.     NSLog(@"return value");
  91.     return nil;
  92. }
  93.  
  94.  
  95.  
  96. - (id)withOperation {
  97.    
  98.     NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  99.  
  100.     NSBlockOperation* theOperationBlock = [NSBlockOperation blockOperationWithBlock: ^{
  101.         NSLog(@"operation block");
  102.     }];
  103.    
  104.     [queue addOperation:theOperationBlock];
  105.     [queue waitUntilAllOperationsAreFinished];
  106.    
  107.     NSLog(@"return value");
  108.    
  109.     return nil;
  110. }
  111.  
  112. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement