[Hereinafter, C++ terms]
I have a thread A and thread B that share access to the integer value P. Thread A initializes this value and updates it while running. Then thread A completes. Thread B waits for thread A to complete (standard OS API call, whatever OS is used) and wants to read P.
Does thread B need a memory barrier to read a coherent, last set by thread A, value of P? Is there a possibility that when OS API says "thread A finished", the changes of memory it modified are not visible to other threads yet?
Note that there only one thread writing the value here, which may or may not distinguish this question from "Is there an implicit memory barrier with synchronized-with relationship on thread::join?" asked before. My gut feeling tells me the answer should be the same, but...
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Difference between Thread#run and Thread#wakeup?
- Selecting only the first few characters in a strin
- Java/Spring MVC: provide request context to child
- What exactly do pointers store? (C++)
join
synchronizes with the thread that callsjoin
. That is, all writes thatA
makes will become visible toB
whenB
callsA.join()
.You can think of this as
A
executingstd::atomic_thread_fence(memory_order_release)
as it finishes andB
executingstd::atomic_thread_fence(std::memory_order_acquire
asA
joins.Yes, but they are implicit in
join
and you do not have to write them.Threads other than the caller of
join
will need additional synchronization, e.g.std::condition_variable
orstd::atomic_thread_fence(std::memory_order_acquire);