A cross-platform .NET Standard 1.5 library to get the cache line size of the processor, in bytes. Windows, Linux, and macOS supported.
usingSystem;usingNickStrupat;classProgram{staticvoidMain(string[]args){Console.WriteLine(CacheLine.Size);// print the cache line size in bytesvararray=newCacheLineAlignedArray<string>(10);Interlocked.Exchange(refarray[0],"Hello");// all threads can now see the latest value at `array[0]` without risk of ruining performance with false-sharing// This can be used to build collections which share elements across threads at the fastest possible synchronization.}// An array-like type where each element is on it's own cache-line. This is a building block for avoiding false-sharing.publicstructCacheLineAlignedArray<T>whereT:class{privatereadonlyT[]buffer;publicCacheLineAlignedArray(Int32size)=>buffer=newT[Multiplier*size];publicInt32Length=>buffer.Length/Multiplier;publicrefTthis[Int32index]=>refbuffer[Multiplier*index];privatestaticreadonlyInt32Multiplier=CacheLine.Size/IntPtr.Size;}}- https://github.com/NickStrupat/CacheLineSize for the equivalent C function
- Create an issue
- Let's find some point of agreement on your suggestion.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request :D