Dukales

r/w test

May 17th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -o errexit
  4. set -o verbose
  5. set -o xtrace
  6.  
  7. g++ -x c++ - -std=gnu++1y -Wall -Wextra -Werror -Wconversion <<__EOF && ./a && echo -e "\e[1;31mOK\e[0m" || echo -e "\e[1;31mfailed!\e[0m"
  8. #include <iostream>
  9. #include <thread>
  10. //#include <mutex>
  11. #include <limits>
  12. #include <cstdint>
  13. #include <cstdlib>
  14.  
  15. using T = std::uintmax_t;
  16. volatile T globalvalue;
  17.  
  18. template< bool upper >
  19. struct reader
  20. {
  21.  
  22.     static
  23.     T constexpr value = upper ? 0 : ~static_cast< T >(0);
  24.  
  25.     void operator () () const
  26.     {
  27.         for (;;) {
  28.             globalvalue = value;
  29.             //std::cout << std::boolalpha << upper << std::endl;
  30.         }
  31.     }
  32.    
  33. };
  34.  
  35. int main() // __attribute__((__noreturn__))
  36. {
  37.     static_assert(std::is_unsigned< T >::value, "Type T is erroneously signed!");
  38.     std::thread r0((reader< true >()));
  39.     std::thread r1((reader< false >()));
  40.     for (;;) {
  41.         volatile T copyvalue = globalvalue;
  42.         if ((copyvalue == reader< true >::value) != (copyvalue == reader< false >::value)) {
  43.             //std::cout << std::boolalpha << (copyvalue == reader< true >::value) << std::endl;
  44.         } else {
  45.             std::cerr << "!" << copyvalue << std::endl;
  46.         }
  47.     }
  48.     return EXIT_SUCCESS;
  49. }
  50.  
  51. __EOF
Advertisement
Add Comment
Please, Sign In to add comment