Advertisement
MrRockchip

linux_threads.c

Feb 28th, 2013 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. /* Taken from http://www.thegeekstuff.com/2012/04/create-threads-in-linux/ */ /* */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <pthread.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8.  
  9. pthread_t tid[2];
  10.  
  11. void* doSomeThing(void *arg)
  12. {
  13.     unsigned long i = 0;
  14.     pthread_t id = pthread_self();
  15.  
  16.     if(pthread_equal(id,tid[0]))
  17.     {
  18.         printf("\n First thread processing\n");
  19.     }
  20.     else
  21.     {
  22.         printf("\n Second thread processing\n");
  23.     }
  24.  
  25.     for(i=0; i<(0xFFFFFFFF);i++);
  26.  
  27.     return NULL;
  28. }
  29.  
  30. int main(void)
  31. {
  32.     int i = 0;
  33.     int err;
  34.  
  35.     while(i < 2)
  36.     {
  37.         err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
  38.         if (err != 0)
  39.             printf("\ncan't create thread :[%s]", strerror(err));
  40.         else
  41.             printf("\n Thread created successfully\n");
  42.  
  43.         i++;
  44.     }
  45.  
  46.     sleep(5);
  47.     return 0;
  48. }
  49.  
  50. /* Output: $ ./threads
  51. Thread created successfully
  52. First thread processing
  53. Thread created successfully
  54. Second thread processing */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement