--sas

thread1

Nov 16th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int a = 10;
  6.  
  7. void *mythread1(void *dummy)
  8. {
  9.  
  10.    pthread_t mythid;
  11.  
  12.    mythid = pthread_self();
  13.  
  14.    a += 10;
  15.  
  16.    printf("Thread %lu, result = %d\n", mythid, a);
  17.  
  18.    return NULL;
  19. }
  20.  
  21.  
  22.  
  23. void *mythread2(void *dummy)
  24. {
  25.  
  26.    pthread_t mythid;
  27.  
  28.    mythid = pthread_self();
  29.  
  30.    a *= 3;
  31.  
  32.    printf("Thread %lu, result = %d\n", mythid, a);
  33.  
  34.    return NULL;
  35. }
  36.  
  37. void *mythread3(void *dummy)
  38. {
  39.  
  40.    pthread_t mythid;
  41.  
  42.    mythid = pthread_self();
  43.  
  44.    a %= 7;
  45.  
  46.    printf("Thread %lu, result = %d\n", mythid, a);
  47.  
  48.    return NULL;
  49. }
  50.  
  51. int main()
  52. {
  53.    pthread_t thid1, thid2, thid3;
  54.  
  55.    int result1 = pthread_create (&thid1, (pthread_attr_t *)NULL, mythread1, NULL);
  56.    if(result1 != 0) { printf ("Error on thread create, return value = %d\n", result1); exit(-1); }
  57.    printf("Thread has been created, thid = %lu\n", thid1);
  58.  
  59.    int result2 = pthread_create( &thid2, (pthread_attr_t *)NULL, mythread2, NULL);
  60.    if(result2 != 0) { printf ("Error on thread create, return value = %d\n", result2); exit(-1); }
  61.    printf("Thread has been created, thid = %lu\n", thid2);
  62.  
  63.    int result3 = pthread_create( &thid3, (pthread_attr_t *)NULL, mythread3, NULL);
  64.    if(result3 != 0){ printf ("Error on thread create, return value = %d\n", result3); exit(-1); }
  65.    printf("Thread has been created, thid = %lu\n", thid3);
  66.  
  67.    pthread_join(thid1, (void **)NULL);
  68.    pthread_join(thid2, (void **)NULL);
  69.    pthread_join(thid3, (void **)NULL);
  70.  
  71.    return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment