
/* TreeSize for Unix  http://treesize.sf.net/
 * Copyright (c) 2006-2007 Marcos Diez <marcos_AT_unitron.com.br>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>

int usleep(int usec);

#include <stdlib.h>
#include <time.h>

#define GRANDE 100

typedef struct _TARGS{
  int i;
} TARGS;

int n=100;
pthread_mutex_t meu_mutex = PTHREAD_MUTEX_INITIALIZER;

void *myThead( void *par){
  int local = *(int *) par;
  double demora=0;
  unsigned int i;
  pthread_mutex_lock( &meu_mutex );
  for(  i = 1 ; i < rand();  i +=i ){
    demora += sqrt(  i ) ;
    usleep( rand()%50000 );
  }


  printf("Sou a thread %3d, %3d  %10d %f\n" , local  , n++ ,  0 ,demora );
  n=local;
  pthread_mutex_unlock( &meu_mutex );

  return 0;
}

int main(){

  int max;
  int i;

  TARGS dados[GRANDE];
  pthread_t id[GRANDE];

  pthread_attr_t atributo;
  pthread_attr_init( &atributo );
  pthread_attr_setdetachstate( &atributo , PTHREAD_CREATE_JOINABLE );


  
  srand( (unsigned int) time( NULL ) );
  
  printf("MAX?\n");
  scanf("%d" , &max );

  
  for( i=0; i < max ; i++ ){
    dados[i].i=i;
    pthread_create( &id[i] , &atributo , *myThead , (void *) &dados[i] );
  }
  printf("blah\n");
  getchar();  getchar();
  printf("valor de n:%d\n" , n );
  return 0;
}
