Notification texts go here Contact Us Buy Now!

Why does C/C++ not have a atomic flag test_and_clear?

Why doesn't C/C++ have an atomic flag test_and_clear()?

The atomic_flag type in C/C++ provides a minimal set of lock-free atomic operations, including test_and_set() and clear(). However, there is no test_and_clear() operation defined for atomic_flag.

The reason for this is that test_and_clear() is not a necessary operation for lock-free synchronization. In fact, it can be implemented using test_and_set() and clear(), as follows:

bool test_and_clear(std::atomic_flag& flag) {
  bool old_value = flag.test_and_set();
  flag.clear();
  return old_value;
}

Since test_and_clear() can be implemented using other atomic operations, there is no need to include it as a separate operation in the atomic_flag type.

Another reason why test_and_clear() is not included in atomic_flag is that it is not as efficient as test_and_set() and clear(). The test_and_set() operation only needs to read and write a single memory location, while the test_and_clear() operation needs to read and write two memory locations.

For these reasons, the C/C++ standard does not define a test_and_clear() operation for atomic_flag.

Related Questions

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.