boost::shared_ptr<A> a = boost::shared_ptr<A>(new A);
a->i = 2;
strand.post([a](){assert(a->i == 2)});
or
io_service.post([a](){assert(a->i == 2)});
When I post a handler to strand or io_service in thread1, does the thread2 which execute handler see data changes before the post?
Java has similar thing Executor which make happen-before relationship:
Actions in a thread prior to the submission of a Runnable to an Executor happen-before its execution begins. Similarly for Callables submitted to an ExecutorService.
What about asio?
Notice that this question does not ask the execute order of handlers which been post to io_service or strand. Asio doc says if using strand, post(a) happen-before post(b). But this question ask: Does actions before(coding order) post(a) happen-before post(a)(handler a's execute)? This is about memory model. Let me clarify:
// in thread1
global variable a = 111;
start a new thread2 which will access global variable a
We know actions in thread1 before start thread2 happen-before thread2 start. That means: 1, final execute order is same as code order(compiler or CPU will not change the order). 2, global variable a
value set action will flush to main memory (or sync to thread2's CPU cache), then thread2 will updated to newest value of global variable a
(of course, till it start).
asio's post is similar with this example, so I ask this question. I think asio's post should make happen-before relationship, or else it will be useless. But I am not sure, and I want to know how asio makes it.
To make happen-before, there is 3 way: 1, thread start, join. 2, lock and unlock a mutex. 3, memory barrier.