Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <climits>
- long fetch_add(long& gcount, long addend)
- {
- long lcount = gcount;
- gcount += addend;
- return lcount;
- }
- int main()
- {
- long m_count = LONG_MAX;
- std::cout << "m_count = " << m_count << "\n";
- // simulate three concurret readers
- fetch_add(m_count, -3);
- std::cout << "m_count = " << m_count << ", 3 readers\n";
- // now m_count = LONG_MAX - 3
- // simulate a writer. There can only be a single writer.
- long count = fetch_add(m_count, -LONG_MAX);
- std::cout << "m_count = " << m_count << ", 3 readers in write mode\n";
- if (count < LONG_MAX)
- {
- std::cout << "\nReaders Detected!\n";
- long readers = LONG_MAX - count;
- std::cout << "count = " << count << "\n";
- std::cout << "readers = " << readers << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment