#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <dispatch/dispatch.h>

#define TAMANHO 100000

int main(int argc, char *argv[])
{
  srand (time(NULL));
  uint32_t quantidade, k;
  if (argc==2) quantidade = atoi(argv[1]);
  else quantidade = 20;
  
  dispatch_queue_t globalFila = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
  dispatch_group_t grp = dispatch_group_create();
  
  for (k = 0; k < quantidade; k++) {
    dispatch_group_async(grp, globalFila, ^{
      int i;
      double result = 0;
      double a, b;
      for ( i = 0; i < TAMANHO; i++){
        a = (double)rand();
        b = (double)rand();
        result += a * b;
      }	
    });
  }
  dispatch_group_wait(grp, DISPATCH_TIME_FOREVER);
  
  return 0;
}