Guest User

Untitled

a guest
Aug 10th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3.  
  4. volatile int x = 0;
  5.  
  6. void *thread1(void *threadarg)
  7. {
  8.    for (int i = 0; i < 1000000000; i++) {
  9.       x++
  10.       printf ("Thread 1: %d\n", x); // mach das besser weg
  11.     }
  12.     printf ("Thread 1 Done: %d\n", x);
  13.     pthread_exit(NULL);
  14. }
  15.  
  16. void *thread2(void *threadarg)
  17. {
  18.    for (int i = 0; i < 1000000000; i++) {
  19.       x++
  20.       printf ("Thread 2: %d\n", x); // mach das besser weg
  21.     }
  22.     printf ("Thread 2 Done: %d\n", x);
  23.     pthread_exit(NULL);
  24. }
  25.  
  26. int main() {
  27.    pthread_t threads[2]; // aus dem internet geklaut, brauche ich das?
  28.    long t;
  29.    pthread_create(&threads[0], NULL, thread1, (void *)t);
  30.    pthread_create(&threads[1], NULL, thread2, (void *)t);
  31.    
  32.     void *status;
  33.    pthread_join(thread[0], &status);
  34.    pthread_join(thread[1], &status);
  35.    printf ("Both threads done");
  36.    
  37.    pthread_exit(NULL);
  38. }
Add Comment
Please, Sign In to add comment