c++ - Effect of std::memory_order_acq_rel on non-atomic variable read in other thread -


i think understand semantics of various memory_order flags in c++ atomic library.

however, i'm confused following situation:

suppose have 2 threads - thread a, "main execution" thread, , thread b, arbitrary thread part of thread pool tasks can scheduled , run.

if perform "read-write-update" atomic operation using std::memory_order_acq_rel, perform non-atomic write on boolean variable, non-atomic write visible other threads? think answer no, unless other threads access atomic variable did "read-write-update" operation.

so, example, given global std::atomic_flag variable x, global bool value b, , thread pool object threadpool has member function dispatch, execute arbitrary function handlers in thread:

if (!x.test_and_set(std::memory_order_acq_rel) {    if (some_condition) b = true;    threadpool.dispatch([]() {        // executes in thread b       if (b) { /* */ } // guaranteed see changes b?    }); } 

so in example, code inside lambda function executed in different thread. thread going see (non-atomic) update b made in first thread? note second thread not access atomic_flag, understanding changes b not seen in second thread.

is understanding correct here? , if so, using std::memory_order_seq_cst change that?

correct implementation of dispatch method in threadpool should provide happens-before relation between operations executed caller before method call , operations executed function(lambda in case), passed method.

so, auxiliary thread, executed lambda function, see value of b, assigned main thread.

without happens-before order, way garantee immediate visibility of variable modification use std::memory_order_seq_cst both modification , reading. see, e.g., this question.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -