SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
Diego Chohfi Turini


                          Paralelismo transparente
                       GCD(Grande Central Dispatcher)

                                               [self SP];




                                               Diego Chohfi - @dchohfi
Monday, March 19, 12
“Quando precisamos de paralelismo temos n
                  problemas, e quando usamos temos n^2
                  problemas” - @qmx



                                             Diego Chohfi - @dchohfi
Monday, March 19, 12
Primeiro:
                    Blocos         ^



                               Diego Chohfi - @dchohfi
Monday, March 19, 12
Véi, na boa.. bloco?

       retorno                paramêtro     nome param

   int (^bloco)(int) = ^(int numero){ return numero *10;};


                       variável                     corpo do broco
                                    broco


                                                   Diego Chohfi - @dchohfi
Monday, March 19, 12
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));


                                                       Diego Chohfi - @dchohfi
Monday, March 19, 12
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");
                  }
              }];




  int variavelExterna = 10;

  void (^block)(int) = ^(int mother){
      NSLog(@"OF JOBS %i, guardei escopo!", modern * variavelExterna);
  };

  block(10);


                                                                 Diego Chohfi - @dchohfi
Monday, March 19, 12
GCD, it’s all about queueing
                               (based on kqueue)




                                                   Diego Chohfi - @dchohfi
Monday, March 19, 12
dispatch_queue_t queue = dispatch_get_global_queue(
                                      DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);




                                         #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




      dispatch_async(queue, ^{
              NSLog(@"Calculando o tamanho do universo, no iPhone 3");
      });




                                                                         Diego Chohfi - @dchohfi
Monday, March 19, 12
Criando queue’s
        dispatch_queue_t minhaQueue = dispatch_queue_create("br.com.caelum", NULL);

        dispatch_async(minhaQueue, ^{

        });




                                                         Diego Chohfi - @dchohfi
Monday, March 19, 12
Alterando views, cuidado!

                       dispatch_async(queue, ^{
                         dispatch_async(queue, ^{
                            [NSThread sleepForTimeInterval:2];
                              [NSThread sleepForTimeInterval:2];
                            dispatch_async(dispatch_get_main_queue(), ^{
                              asyncLabel.text = = @"UNIVERSO CALCULADO";
                               asynchLabel.text @"UNIVERSO CALCULADO";
                           });
                            });
                       });
                         syncLabel.text = @"Calculando o universo";
                       syncLabel.text = @"Calculando o universo";




                                                        Diego Chohfi - @dchohfi
Monday, March 19, 12
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");
      });




                                                Diego Chohfi - @dchohfi
Monday, March 19, 12
Valores fora do escopo


                       int valorMagico = 0;
                       __block
                       dispatch_async(queue,^{
                          [NSThread sleepForTimeInterval:1];
                          for (int i=0; i<10; i++) {
                            valorMagico++;
                          }
                       });




                                                    Diego Chohfi - @dchohfi
Monday, March 19, 12
Batch queueing


      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
      });




                                              Diego Chohfi - @dchohfi
Monday, March 19, 12

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Palestra sobre Grand Central Dispatch no [self sp]

  • 1. Diego Chohfi Turini Paralelismo transparente GCD(Grande Central Dispatcher) [self SP]; Diego Chohfi - @dchohfi Monday, March 19, 12
  • 2. “Quando precisamos de paralelismo temos n problemas, e quando usamos temos n^2 problemas” - @qmx Diego Chohfi - @dchohfi Monday, March 19, 12
  • 3. Primeiro: Blocos ^ Diego Chohfi - @dchohfi Monday, March 19, 12
  • 4. Véi, na boa.. bloco? retorno paramêtro nome param int (^bloco)(int) = ^(int numero){ return numero *10;}; variável corpo do broco broco Diego Chohfi - @dchohfi Monday, March 19, 12
  • 5. 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)); Diego Chohfi - @dchohfi Monday, March 19, 12
  • 6. 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"); } }]; int variavelExterna = 10; void (^block)(int) = ^(int mother){ NSLog(@"OF JOBS %i, guardei escopo!", modern * variavelExterna); }; block(10); Diego Chohfi - @dchohfi Monday, March 19, 12
  • 7. GCD, it’s all about queueing (based on kqueue) Diego Chohfi - @dchohfi Monday, March 19, 12
  • 8. dispatch_queue_t queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); #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 dispatch_async(queue, ^{ NSLog(@"Calculando o tamanho do universo, no iPhone 3"); }); Diego Chohfi - @dchohfi Monday, March 19, 12
  • 9. Criando queue’s dispatch_queue_t minhaQueue = dispatch_queue_create("br.com.caelum", NULL); dispatch_async(minhaQueue, ^{ }); Diego Chohfi - @dchohfi Monday, March 19, 12
  • 10. Alterando views, cuidado! dispatch_async(queue, ^{ dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; [NSThread sleepForTimeInterval:2]; dispatch_async(dispatch_get_main_queue(), ^{ asyncLabel.text = = @"UNIVERSO CALCULADO"; asynchLabel.text @"UNIVERSO CALCULADO"; }); }); }); syncLabel.text = @"Calculando o universo"; syncLabel.text = @"Calculando o universo"; Diego Chohfi - @dchohfi Monday, March 19, 12
  • 11. 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"); }); Diego Chohfi - @dchohfi Monday, March 19, 12
  • 12. Valores fora do escopo int valorMagico = 0; __block dispatch_async(queue,^{ [NSThread sleepForTimeInterval:1]; for (int i=0; i<10; i++) { valorMagico++; } }); Diego Chohfi - @dchohfi Monday, March 19, 12
  • 13. Batch queueing 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 }); Diego Chohfi - @dchohfi Monday, March 19, 12