Chris_M_Thomasson

where is the UB?

Feb 17th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <climits>
  3.  
  4.  
  5. long fetch_add(long& gcount, long addend)
  6. {
  7. long lcount = gcount;
  8. gcount += addend;
  9. return lcount;
  10. }
  11.  
  12.  
  13. int main()
  14. {
  15. long m_count = LONG_MAX;
  16. std::cout << "m_count = " << m_count << "\n";
  17.  
  18. // simulate three concurret readers
  19. fetch_add(m_count, -3);
  20. std::cout << "m_count = " << m_count << ", 3 readers\n";
  21.  
  22. // now m_count = LONG_MAX - 3
  23.  
  24. // simulate a writer. There can only be a single writer.
  25. long count = fetch_add(m_count, -LONG_MAX);
  26. std::cout << "m_count = " << m_count << ", 3 readers in write mode\n";
  27.  
  28. if (count < LONG_MAX)
  29. {
  30. std::cout << "\nReaders Detected!\n";
  31. long readers = LONG_MAX - count;
  32. std::cout << "count = " << count << "\n";
  33. std::cout << "readers = " << readers << "\n";
  34. }
  35.  
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment