"GetMisalignedHeapLong Explained"

// Returns a pointer to a long that spans two cache lines (e.g. it flows over the end of one cache line on to the next)
private static long* GetMisalignedHeapLong() {
	const int MISALIGNMENT = -3; // We eventually want to move the long 3 bytes backwards past the start of the target cache line
	const int X64_CACHE_LINE_SIZE = 64; // The size of a cache line on my target architecture
	const int WIN_ALLOC_ALIGN = 8; // The default alignment that the windows allocator provides for all memory allocations
	
	// Reserve enough memory so that we can place the beginning of the long where we need to.
	// We need:
	//		* sizeof(long) bytes to fit the long itself,
	//		* Potentially the size of a cache line in the worst case in order to be able to move our long pointer back to the previous one,
	//		* Enough room to move the long back further up to (sizeof(long) - 1) bytes, depending on our MISALIGNMENT value
	//		* An additional 8 bytes to account for the windows allocator's built-in alignment
	IntPtr reservedMemory = Marshal.AllocHGlobal(new IntPtr(sizeof(long) + X64_CACHE_LINE_SIZE + (sizeof(long) - 1) + WIN_ALLOC_ALIGN));
	
	// Find the start of a cache line. This will be the last byte-address in our reserved memory for which (address % 64) == 0
	// We're using the fact that integer division discards remainder/fractional values to find this boundary
	IntPtr cacheLineStart = (IntPtr) ((((long) reservedMemory + (X64_CACHE_LINE_SIZE - 1)) / X64_CACHE_LINE_SIZE) * X64_CACHE_LINE_SIZE);
	
	// Return a pointer that points to where the cache line starts minus 3 bytes (according to MISALIGNMENT)
	return (long*) (cacheLineStart + MISALIGNMENT);
}


Code snippet taken from "Common Multithreading Mistakes in C# - IV: Everything Else".