Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1287

Why set the stop flag using `memory_order_seq_cst`, if you check it with `memory_order_relaxed`?

$
0
0

Herb Sutter, in his "atomic<> weapons" talk, shows several example uses of atomics, and one of them boils down to following: (video link, timestamped)

  • A main thread launches several worker threads.

  • Workers check the stop flag:

    while (!stop.load(std::memory_order_relaxed)){    // Do stuff.}
  • The main thread eventually does stop = true; (note, using order=seq_cst), then joins the workers.

Sutter explains that checking the flag with order=relaxed is ok, because who cares if the thread stops with a slightly bigger delay.

But why does stop = true; in the main thread use seq_cst? The slide says that it's purposefully not relaxed, but doesn't explain why.

It looks like it would work, possibly with a larger stopping delay.

Is it a compromise between performance and how fast other threads see the flag? I.e. since the main thread only sets the flag once, we might as well use the strongest ordering, to get the message across as quickly as possible?


Viewing all articles
Browse latest Browse all 1287

Trending Articles



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