I create a very fast multi-threaded system for modeling discrete events. The core of the structure uses atomatics and contactless programming techniques to achieve very fast execution in many threads. This requires me to bind some variables to cache lines and pad the remaining cache line space so that I have no argument regarding the cache line. Here is how I do it:
constexpr u64 CLPAD(u64 _objSize) {
return ((_objSize / CACHELINE_SIZE) * CACHELINE_SIZE) +
(((_objSize % CACHELINE_SIZE) > 0) * CACHELINE_SIZE) -
_objSize;
}
alignas(CACHELINE_SIZE) MyObject myObj;
char padding[CLPAD(sizeof(myObj))];
This works fine for me, but today I came across a problem when I used this methodology for a new type of object. The CLPAD () function returns the number of characters required to enter an input type to the next cache line. However, if I insert a type whose size is exactly equal to the number of cache lines, CLPAD returns 0. If you try to create an array of zero size, you will get this warning / error:
ISO C++ forbids zero-size array 'padding'
I know that I can modify CLPAD () to return CACHELINE_SIZE in this case, but then I burn the line in the cache without spaces.
How can I make a fill-in declaration disappear if CLPAD returns 0?
source
share