Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.m
  3. //  ls6objc
  4. //
  5. //  Created by Юрий Новокрещенов on 11/11/2018.
  6. //  Copyright © 2018 Yuri Novokreschenov. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "Arithmetic.h"
  11. #import "Operation.h"
  12.  
  13. int main(int argc, const char * argv[]) {
  14.     @autoreleasepool {
  15.         int a = 40;
  16.         int b = 10;
  17.        
  18. //        dispatch_queue_t mainQueue = dispatch_get_main_queue();
  19.        
  20.         dispatch_group_t group = dispatch_group_create();
  21.         dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
  22.  
  23.         __block int resultSum;
  24.         __block int resultSubstraction;
  25.         __block int resultMultiplication;
  26.    
  27.         dispatch_group_async(group, queue, ^{
  28.             resultSum = [Arithmetic beginWithAction:ActionTypeSum firstNumber:a secondNumber:b];
  29.         });
  30.  
  31.         dispatch_group_async(group, queue, ^{
  32.             resultSubstraction = [Arithmetic beginWithAction:ActionTypeSubstraction firstNumber:a secondNumber:b];
  33.         });
  34.        
  35.         dispatch_group_notify(group, queue, ^{
  36.             NSLog(@"Сумма = %i", resultSum);
  37.             NSLog(@"Разность = %i", resultSubstraction);
  38.         });
  39.        
  40.         NSLog(@"Исходные данные: a = %i, b = %i", a, b);
  41.  
  42.         //ставим барьер
  43.         dispatch_queue_t queue1 = dispatch_queue_create("my.first.queue", NULL);
  44.         dispatch_async(queue1, ^{
  45.             resultMultiplication = [Arithmetic beginWithAction:ActionTypeMultiplication firstNumber:a secondNumber:b];
  46.         });
  47.        
  48.         dispatch_barrier_async(queue1, ^{
  49.             NSLog(@"Произведение = %i", resultMultiplication);
  50.         });
  51.  
  52.         // NSOperationQueue
  53.         __block int resultDivision;
  54.         NSOperationQueue *queue2 = [[NSOperationQueue alloc] init];
  55.        
  56.         //создади блок в котором запишем операцию
  57.         int(^op0)(void) = ^int()
  58.         {
  59.             NSLog(@"Выполнился блок 0");
  60.             return [Arithmetic beginWithAction:ActionTypeDivision firstNumber:a secondNumber:b];
  61.         };
  62.        
  63.         int(^op1)(void) = ^int{
  64.             int resultRemainderOfTheDivision = [Arithmetic beginWithAction:ActionTypeRemainderOfTheDivision firstNumber:a secondNumber:b];
  65.             NSLog(@"Выполнился блок 1 %i", resultRemainderOfTheDivision);
  66.             return resultRemainderOfTheDivision;
  67.         };
  68.        
  69.         // хочу в operation передать действие через замыкание (то есть замыкание это написать в main, а в operation его отправить как переменную).
  70.        
  71.         Operation *operation0 = [[Operation alloc] init];
  72.         operation0.someBlock = op0;
  73.         [operation0 someBlock];
  74.         Operation *operation1 = [[Operation alloc] init];
  75.         operation1.someBlock = op1;
  76.        
  77.         [operation0 addDependency:operation1];
  78.         [queue2 addOperation:operation0];
  79.         [queue2 addOperation:operation1];
  80.        
  81.         [queue2 addOperationWithBlock:^{
  82.             resultDivision = [Arithmetic beginWithAction:ActionTypeDivision firstNumber:a secondNumber:b];
  83.             NSLog(@"Целочисленное деление = %i", resultDivision);
  84.         }];
  85.  
  86.         int resultRemainderOfTheDivision = [Arithmetic beginWithAction:ActionTypeRemainderOfTheDivision firstNumber:a secondNumber:b];
  87.         NSLog(@"ActionTypeRemainderOfTheDivision = %i", resultRemainderOfTheDivision);
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement