Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15881

Why std::mutex of c++11 has no memory order?

$
0
0

I mean compared with c++11 atomic, for example:

#include <iostream>#include <thread>#include <atomic>std::atomic<int> counter(0);void incrementCounter() {    for (int i = 0; i < 1000; ++i) {        counter.fetch_add(1, std::memory_order_relaxed);    }}int main() {    std::thread t1(incrementCounter);    std::thread t2(incrementCounter);    t1.join();    t2.join();    std::cout << "Final counter value: " << counter << std::endl;    return 0;}

In the usage of std::atomic, we need to pass memory order parameters. Why doesn't std::mutex have this parameter and what's the default memory order of std::mutex?


Viewing all articles
Browse latest Browse all 15881

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>