Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define TOTAL_TH 10
  5. long exit_codes[TOTAL_TH];
  6. long input_data[TOTAL_TH];
  7. void F_thread(void *quiensoy)
  8. {
  9. long quien_soy;
  10. quien_soy=*((long *)quiensoy);
  11. fprintf(stdout,"Soy el thread con ID local %ld\n",quien_soy);
  12. /* Los threads pares devolveran 0 y los impares 1 */
  13. exit_codes[quien_soy]=quien_soy%2;
  14. pthread_exit(&exit_codes[quien_soy]);
  15. }
  16. void main(int argc,char *argv[])
  17. {
  18. pthread_t th_list[TOTAL_TH];
  19. int i,*return_value,ret;
  20. /* Creamos 10 threads */
  21. for (i=0;i<TOTAL_TH;i++)
  22. {
  23. input_data[i]=i;
  24. if ((ret=pthread_create(&th_list[i],NULL,(void *)F_thread,(void *)&input_data[i]))!=0)
  25. {
  26. fprintf(stdout,"create: error %d in th %d\n",ret,i);
  27. }
  28. }
  29. /*Esperamos a que acaben */
  30. for (i=0;i<TOTAL_TH;i++)
  31. {
  32. if (pthread_join(th_list[i],(void **)&return_value)!=0)
  33. {
  34. fprintf(stdout,"Error en join th %d\n",i);
  35. }else{
  36. fprintf(stdout,"thread %d acaba con estado %d\n",i,*return_value);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement