Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env sh
- set -o errexit
- set -o verbose
- g++ -x c++ - -std=gnu++11 -pthread -o a <<__EOF && ./a && echo OK || echo failed!
- #include <iostream>
- #include <thread>
- #include <limits>
- #include <cstdint>
- #include <cstdlib>
- typedef std::uintmax_t T;
- std::size_t const half_digits = std::numeric_limits< T >::digits / 2;
- T const a = (std::numeric_limits< T >::max() << half_digits);
- T const b = (std::numeric_limits< T >::max() >> half_digits);
- volatile T x;
- struct thread_flipper
- {
- void operator () () const
- {
- for (;;) {
- x = a;
- x = b;
- }
- }
- };
- struct thread_checker
- {
- void operator () () const
- {
- for (;;) {
- volatile auto const y = x;
- if ((y != a) && (y != b)) {
- std::cerr << "x read partitionally" << std::endl;
- }
- }
- }
- };
- int main()
- {
- //std::cout << std::numeric_limits< decltype(x) >::digits << std::endl;
- //std::cin.get();
- x = a;
- std::thread t1((thread_flipper()));
- std::thread t2((thread_checker()));
- t1.join();
- t2.join();
- return EXIT_SUCCESS;
- }
- __EOF
Advertisement
Add Comment
Please, Sign In to add comment