Advertisement
paulogp

Threads (v. 1.0.0)

Jul 14th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 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. void *print_xs(void *unused)
  12. {
  13.     while (1)
  14.     {
  15.         fputc('x', stderr);
  16.     }
  17.    
  18.     return NULL;
  19. }
  20.  
  21. int main (int argc, const char * argv[])
  22. {
  23.     pthread_t the_thread_id;
  24.    
  25.     // cria uma thread para executar print_xs em
  26.     // simultaneo com a funcao main
  27.     pthread_create(&the_thread_id, NULL, print_xs, NULL);
  28.    
  29.     // nao queremos esperar pela conclusao da thread
  30.     pthread_detach(the_thread_id);
  31.    
  32.     // while (1) infinite loop
  33.     for (int i = 0; i < 10000; i++)
  34.     {
  35.         fputc('o', stderr);
  36.     }
  37.    
  38.     printf("\n");
  39.    
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement