Advertisement
paulogp

Threads (v. 1.0.1)

Jul 14th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. // multiplas tarefas (thread)
  2.  
  3. // Apple Xcode
  4. // paulogp
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <time.h>
  9. #include <pthread.h>
  10.  
  11. struct char_print_parms
  12. {
  13.     char character;
  14.     int count;
  15. };
  16.  
  17. void* char_print (void* parameters)
  18. {
  19.     int i;
  20.    
  21.     struct char_print_parms* p = (struct char_print_parms*) parameters;
  22.    
  23.     for (i = 0; i < p->count; i++)
  24.     {
  25.         fputc(p->character, stderr);
  26.     }
  27.    
  28.     return NULL;
  29. }
  30.  
  31. int main (int argc, const char * argv[])
  32. {
  33.     pthread_t the_thread_id_1, the_thread_id_2;
  34.    
  35.     struct char_print_parms t1_args;
  36.     struct char_print_parms t2_args;
  37.    
  38.     t1_args.character = 'x';
  39.     t1_args.count = 30000;
  40.     pthread_create(&the_thread_id_1, NULL, char_print, &t1_args);
  41.    
  42.     t2_args.character = 'o';
  43.     t2_args.count = 20000;
  44.     pthread_create(&the_thread_id_2, NULL, char_print, &t2_args);
  45.    
  46.     // nao queremos que o processo termine antes de ambas
  47.     // as threads terminarem
  48.     pthread_join(the_thread_id_1, NULL);
  49.     pthread_join(the_thread_id_2, NULL);
  50.    
  51.     printf("\ndone\n");
  52.    
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement