Advertisement
Guest User

Untitled

a guest
May 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. #define NCOUNT (100000)
  7. #define TCOUNT (4)
  8.  
  9. volatile uint64_t u = 0;
  10. volatile uint64_t *casvar = &u;
  11.  
  12. void *
  13. thread(void *args) {
  14. volatile uint64_t val = 0;
  15. int i;
  16.  
  17. for (i = 0; i < NCOUNT; i++) {
  18. for (;;) {
  19. __atomic_load(casvar, &val, __ATOMIC_CONSUME);
  20. if (__sync_val_compare_and_swap(casvar, val, val+1) == val)
  21. break;
  22. }
  23. }
  24.  
  25. return (NULL);
  26. }
  27.  
  28. int
  29. main(int argc, char **argv)
  30. {
  31. uint64_t i;
  32. pthread_t tid[TCOUNT];
  33. volatile uint64_t result;
  34.  
  35. for (i = 0; i < TCOUNT; i++)
  36. pthread_create(&tid[i], NULL, thread, (void *)i+10);
  37.  
  38. for (i = 0; i < TCOUNT; i++) {
  39. if (pthread_join(tid[i], NULL))
  40. printf("pthread_join failed %d\n", i+10);
  41. }
  42.  
  43. __atomic_load(casvar, &result, __ATOMIC_CONSUME);
  44. if (result != TCOUNT*NCOUNT) {
  45. printf("womp womp %d %d\n", result, TCOUNT*NCOUNT);
  46. return (1);
  47. }
  48. printf("ok %d %d\n", result, TCOUNT*NCOUNT);
  49. return (0);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement