简体   繁体   中英

Why is cache ping-ponging and issue if different cores have different L1 cache?

I was reading about false sharing and cache ping-ponging when you have multiple threads on different cores trying to use the same cache line, but for different data (like two int values next to each other in an array). In this case the cache line would need to move back and fourth between the cores. What I am confused about is that I thought each core has its own L1 cache so why does it need to share that cache line with the other cores? Wouldn't it just keep its own copy and update that? Also if the cpu is forced to make all of the cache consistent between cores what is the point of having keywords like volatile in c++ (other than maybe preventing the compiler from storing values in registers)?

Fist off you should not use volatile for thread synchronization. For a detailed explanation on that see Why is volatile not considered useful in multithreaded C or C++ programming?

Secondly the reason false sharing is a problem is that when a variable in a cache line is updated the entire cache line is marked as dirty. This then forces the same cache line on all cores to be invalidated as they do not know what part was updated, they just know that the line was updated so it needs to be synchronized.

Intel has a very good article on this called Avoiding and Identifying False Sharing Among Threads

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