#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <pthread.h>

#define TAMANHO 100000

void *thread_func(void *arg);

int main(int argc, char *argv[])
{
  srand (time(NULL));    
  uint32_t quantidade, i;
  pthread_t *threads;

  if (argc==2) quantidade = atoi(argv[1]);
  else quantidade = 20;
  
  threads = (pthread_t*) malloc (quantidade*sizeof(pthread_t));

  for(i=0; i < quantidade; i++) {
    pthread_create(&(threads[i]), NULL, thread_func, NULL);
  }
  
  for(i=0; i < quantidade; i++) {
    pthread_join(threads[i], NULL);
  }
  return 0;
  
}

void *thread_func(void *arg) {
  int i;
  double result = 0;
  double a, b;
  for ( i = 0; i < TAMANHO; i++){
    a = (double)rand();
    b = (double)rand();
    result += a * b;
  }	
  return NULL;
}