dimanche 13 novembre 2022

relaxed memory ordering for thread stop flags

The author in the video on atomic ops has the following snippet. While the load to the stop flag is not relaxed, the store cannot be relaxed. My question is if the reason for store not to be relaxed has to do with it potentially being visible after the join of threads / if there is some other reason?

Worker threads:

while (!stop.load(std::memory_order_relaxed)) {
    // do something (that's independent of stop flag)
}

Main thread:

int main() {
  launch_workers();
  stop = true; // <-- not relaxed
  join_threads();
  // do something  
}

Aucun commentaire:

Enregistrer un commentaire