False sharing is a situation that can occur in a computer program when two applications that are running simultaneously attempt to access information in the same logical memory region that each program or process has stored in its own cache. The data in each application’s cache are copied from a common source, so modifying one cache causes the other to have to be reloaded from the source. The false aspect of the sharing arises when the changes made to the cache line by one program do not actually affect the data that the second program is using, in which case forcing the cache to be reloaded is a waste of system resources and can negatively affect the performance of the program. The problem of false sharing is hard to detect, because it is not a direct result of any code contained within the applications, although there are effective ways to prevent false sharing, such as moving data that are in a currently cached line to a separate line.
One of the primary reasons why false sharing can occur is found in how an operating system or hardware handles reading and writing data. When information from a hard disk or other source is being read or written by a program, it usually is loaded into a temporary cache so it can be accessed quickly. The amount of information that is cached from the source location is called a cache line and is a logical block of memory that normally can be between 2 bytes and 256 bytes in length. Under some operating systems or system architectures, programs are able to run at the same time, such as in parallel processing. This means it is possible for two separate programs to attempt to access information in the same cache line, so each program will have its own copy of the source data, potentially causing changes in one cache not to be reflected in the other cache, invalidating the data it holds.
There are a number of mechanisms employed to handle the situation when two programs attempt to access the same cache line, but the result most often is that one program is forced to reload its cache with the updated information the other program has modified. This type of incident is known as false sharing when the data within the cache line that each program is accessing are not related, so forcing one program to reload the cache is a waste of processing power and other resources. This most often is a problem for a program that will never modify the information in its cache, so there is no risk of the application overwriting changes the other process has made.
Two prevalent ways exist either to prevent or mitigate the effects of false sharing. The first is to add blank information before or after the data in memory, essentially forcing it into a separate cache line that is not being accessed by another program. The second method is to restrict the frequency of reading and writing to the cache line so it is reloaded as little as possible. More complex solutions involve cache-level management or even changes to how an operating system handles sharing.