Do I need a memory barrier to access memory modified by a thread that has ended?

[Further, C ++ terms]

I have thread A and thread B that share access to the integer value P. Thread A initializes this value and updates it while it is running. Thread A then executes. Thread B waits for thread A to finish (a standard API call to OS OS, regardless of the OS used) and wants to read P.

Does memory thread B need to read the coherent, last specified by thread A, value P? Is there a chance that when the OS API says "thread A is finished", the changes in memory that it changed are not yet visible to other threads?

Please note that there is only one line here that may or may not distinguish this question from Is there an implicit memory barrier with synchronized communication on thread :: join?? My gut feeling tells me that the answer should be the same, but ...

+2
source share
1 answer

joinsynchronizes with the thread that is calling join. That is, everyone writes that Amake will become visible to Bwhen it Binvokes A.join().

You can think of it as Aexecution std::atomic_thread_fence(memory_order_release)as it completes, and Bexecution std::atomic_thread_fence(std::memory_order_acquireas it merges A.

Does thread B require a memory barrier?

Yes, but they are implicit in join, and you do not need to write them.

Is it likely that when the OS API says "thread A is finished", the changes in memory that it changed are not yet visible to other threads?

, join, , . std::condition_variable std::atomic_thread_fence(std::memory_order_acquire);

+1

Source: https://habr.com/ru/post/1687579/


All Articles