Cache lines, false sharing and alignment

后端 未结 3 376
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 14:22

I wrote the following short C++ program to reproduce the false sharing effect as described by Herb Sutter:

Say, we want to perform a total amount of WORKLOAD integer op

相关标签:
3条回答
  • 2021-02-05 14:28

    You should be able to request the required alignment from the compiler:

    alignas(64) int arr[PARALELL * PADDING]; // align the array to a 64 byte line
    
    0 讨论(0)
  • 2021-02-05 14:36

    gcc supports an aligned keyword: http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html

    You probably want something like this:

    int arr[PARALLEL * 16] __attribute__ ((aligned (8)));

    This aligns arr to an eight-byte boundary.

    Visual Studio has a similar feature, too: http://msdn.microsoft.com/en-us/library/83ythb65.aspx

    0 讨论(0)
  • 2021-02-05 14:39

    In modern C++ (17 and above) you should use hardware_constructive_interference_size.

    0 讨论(0)
提交回复
热议问题