Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 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. long long int j=0;
  7. int sum=0;
  8.  
  9.  
  10. void* child_fn ( void* arg ) {
  11. pthread_mutex_lock(&mut);
  12. while(j<1000000000) {
  13. sum=(sum+j*j) %1024;
  14. j++;
  15. }
  16. pthread_mutex_unlock(&mut);
  17. return NULL;
  18. }
  19.  
  20. int main (int argc, char **argv) {
  21. int i,a;
  22. a=atoi(argv[1]);
  23. pthread_t child;
  24. for(i=0;i<a;i++) pthread_create(&child, NULL, child_fn, NULL);
  25. pthread_join(child, NULL);
  26. printf("%d\n", sum);
  27. return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement