Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Paralelismo transparente com GCD (Grand Central...

Paralelismo transparente com GCD (Grand Central Dispatcher)

Palestra no segundo encontro do [self SP]; na Caelum :D

Diego Chohfi

March 19, 2012
Tweet

More Decks by Diego Chohfi

Other Decks in Programming

Transcript

  1. Diego Chohfi - @dchohfi Diego Chohfi Turini [self SP]; Paralelismo

    transparente GCD(Grande Central Dispatcher) Monday, March 19, 12
  2. Diego Chohfi - @dchohfi “Quando precisamos de paralelismo temos n

    problemas, e quando usamos temos n^2 problemas”- @qmx Monday, March 19, 12
  3. Diego Chohfi - @dchohfi int (^bloco)(int) = ^(int numero){ return

    numero *10;}; retorno variável paramêtro broco nome param corpo do broco Véi, na boa.. bloco? Monday, March 19, 12
  4. Diego Chohfi - @dchohfi Bloco = tipo typedef double(^Dividir)(double, double);

    @property (copy) Dividir dividir; dividir = ^( double dividendo, double divisor){ return dividendo / divisor; }; NSLog(@"%f.0", self.dividir(10,2)); Monday, March 19, 12
  5. Diego Chohfi - @dchohfi int variavelExterna = 10; void (^block)(int)

    = ^(int mother){ NSLog(@"OF JOBS %i, guardei escopo!", modern * variavelExterna); }; block(10); NSArray *OMG = [[NSArray alloc] initWithObjects:@"MOTHER", @"OF", @"JOBS", nil]; [OMG enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(obj); }]; TWTweetComposeViewController *tweet = [[TWTweetComposeViewController alloc] init]; [tweet setCompletionHandler:^(TWTweetComposeViewControllerResult result){ if(result == TWTweetComposeViewControllerResultDone){ NSLog(@"AI QUE LOUCURA, TWEETEI"); } }]; Monday, March 19, 12
  6. Diego Chohfi - @dchohfi dispatch_queue_t queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue, ^{ NSLog(@"Calculando o tamanho do universo, no iPhone 3"); }); #define! DISPATCH_QUEUE_PRIORITY_HIGH ! 2 #define! DISPATCH_QUEUE_PRIORITY_DEFAULT 0 #define! DISPATCH_QUEUE_PRIORITY_LOW ! (-2) #define! DISPATCH_QUEUE_PRIORITY_BACKGROUND! INT16_MIN Monday, March 19, 12
  7. Diego Chohfi - @dchohfi dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; asyncLabel.text =

    @"UNIVERSO CALCULADO"; }); syncLabel.text = @"Calculando o universo"; Alterando views, cuidado! dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; dispatch_async(dispatch_get_main_queue(), ^{ asynchLabel.text = @"UNIVERSO CALCULADO"; }); }); syncLabel.text = @"Calculando o universo"; Monday, March 19, 12
  8. Diego Chohfi - @dchohfi Criando Grupos dispatch_group_t group = dispatch_group_create();

    dispatch_group_async(group,queue,^{ [NSThread sleepForTimeInterval:1]; NSLog(@"Calculando o tamanho do sol"); }); dispatch_group_async(group,queue,^{ NSLog(@"Esperando a mulher fazer compras"); }); dispatch_group_notify(group,queue,^{ NSLog(@"Será executado após os dois blocos"); }); Monday, March 19, 12
  9. Diego Chohfi - @dchohfi dispatch_async(queue,^{ [NSThread sleepForTimeInterval:1]; for (int i=0;

    i<10; i++) { valorMagico++; } }); Valores fora do escopo __block int valorMagico = 0; Monday, March 19, 12
  10. Diego Chohfi - @dchohfi dispatch_queue_t queue = dispatch_get_global_queue(0, 0); dispatch_apply(10,

    queue, ^(size_t i){ NSLog(@"Numero: %zu", i);//não garante ordem }); Batch queueing Monday, March 19, 12