Dukales

r/w test

Feb 18th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.03 KB | None | 0 0
  1. #!/usr/bin/env sh
  2.  
  3. set -o errexit
  4. set -o verbose
  5.  
  6. g++ -x c++ - -std=gnu++11 -pthread -o a <<__EOF && ./a && echo OK || echo failed!
  7. #include <iostream>
  8. #include <thread>
  9. #include <limits>
  10.  
  11. #include <cstdint>
  12. #include <cstdlib>
  13.  
  14. typedef std::uintmax_t T;
  15.  
  16. std::size_t const half_digits = std::numeric_limits< T >::digits / 2;
  17. T const a = (std::numeric_limits< T >::max() << half_digits);
  18. T const b = (std::numeric_limits< T >::max() >> half_digits);
  19. volatile T x;
  20.  
  21. struct thread_flipper
  22. {
  23.     void operator () () const
  24.     {
  25.         for (;;) {
  26.             x = a;
  27.             x = b;
  28.         }
  29.     }
  30. };
  31.  
  32. struct thread_checker
  33. {
  34.     void operator () () const
  35.     {
  36.         for (;;) {
  37.             volatile auto const y = x;
  38.             if ((y != a) && (y != b)) {
  39.                 std::cerr << "x read partitionally" << std::endl;
  40.             }
  41.         }
  42.     }
  43. };
  44.  
  45. int main()
  46. {
  47.     //std::cout << std::numeric_limits< decltype(x) >::digits << std::endl;
  48.     //std::cin.get();
  49.     x = a;
  50.     std::thread t1((thread_flipper()));
  51.     std::thread t2((thread_checker()));
  52.     t1.join();
  53.     t2.join();
  54.     return EXIT_SUCCESS;
  55. }
  56. __EOF
Advertisement
Add Comment
Please, Sign In to add comment