Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. int N=0;
  7. void *thread(void *arg)
  8. {
  9.    int n,i;
  10.    for (i=0;i<5;i++)
  11.    {
  12.     n=N;
  13.     n++;
  14.     printf("thread() [%lu]\n",(unsigned long)pthread_self());
  15.     fflush(stdout);
  16.     sleep(1);
  17.     N=n;
  18.    }
  19. pthread_exit(NULL);
  20. }
  21.  
  22. int main(void)
  23. {
  24.    pthread_t tid;
  25.    int i;
  26.    if(pthread_create(&tid,NULL,thread,NULL))
  27.    {
  28.       perror("...pthread_create()..."); exit(1);
  29.    }
  30.    for (i=0;i<5;i++)
  31.    {
  32.       N--;
  33.       printf("main()   [%lu]\n", (unsigned long)pthread_self() );
  34.       fflush(stdout);
  35.       sleep(1);
  36.    }
  37.    if(pthread_join(tid,NULL))
  38.    {perror("...pthread_join()...");exit(2);}
  39.    printf("\nglobalnie N=%d, po wykonaniu 5+5 iteracji\n",N);
  40.    return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement