Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4.  
  5. pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  6. int j=0,sum=0;
  7.  
  8.  
  9. void* child_fn ( void* arg ) {
  10. pthread_mutex_lock(&mut);
  11. while(j<100000000) {
  12. sum=(sum+j*j) %1024;
  13. j++;
  14. }
  15. pthread_mutex_unlock(&mut);
  16. return NULL;
  17. }
  18.  
  19. int main (int argc, char **argv) {
  20. int i,a;
  21. a=atoi(argv[1]);
  22. pthread_t child;
  23. for(i=0;i<a;i++) pthread_create(&child, NULL, child_fn, NULL);
  24. pthread_join(child, NULL);
  25. printf("%d\n", sum);
  26. return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement