简体   繁体   中英

How often does processor cache flush?

Say I have a casual single-byte variable. I think on pretty much all systems single-byte operations are atomic, but if not please let me know. Now, say one thread updates this variable. How long should I expect/prepare for this update to appear in the other threads? I know I can put the update around mutexes/locks/barriers to make sure it's synchronized everywhere, but I'm curious about this. The wait time probably varies depending on whether the other threads are on separate processors/cores, and maybe depending on processor type.

Am I being logical for wondering this or have I greatly misunderstood something?

Memory is synchronized as soon as you call a synchronization primitive/memory barrier such as pthread_mutex_lock . Aside from that, you should not assume any synchronization unless you're using C11 atomic types.

In many architectures, the processor won't flush the cache until it has to - to make way for some more-needed data.

However, if the threads are sharing memory space, and you only have a single core, they will be able to see the update "immediately" from the cache. If it's actually been written from the CPU to memory. Which it may not be if the compiler's decided to keep it in a register, in which case your threads will all have their own "local" and incorrect copy.

As others have said, it's an interesting question - but the right answer for synchronising is to use proper synchronisation primitives!

在MIPS架构上有一个同步指令,它作为跨核心的加载存储屏障,即在发出同步之前的所有加载和存储将在任何加载之前发生并在同步之后存储。不确定x86中是否存在等效指令(假设是您正在使用的架构)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM