Why does this snippet fail to produce false sharing effects?

77 Views Asked by At

I tried to make a small snippet that reproduces a false sharing scenario. This is what I came up with:

#include <thread>
#include <cstdlib>
#include <vector>

const size_t s_nNumberOfThreads = 16;

volatile __declspec(align(64)) int s_pValues[s_nNumberOfThreads];

void IncrementValue(size_t nValueIndex)
{
    int nValue = 0;
    s_pValues[nValueIndex] = nValue;
    while (true)
    {
        if (s_pValues[nValueIndex] != nValue)
        {
            break;
        }
        nValue++;
        s_pValues[nValueIndex] = nValue;
    }
    // We messed up the memory
    int i = 3; // Breakpoint here
}

int main(int argc, char** argv)
{
    std::vector<std::thread> Threads;
    size_t nThreadIndex = 0;
    while (nThreadIndex < s_nNumberOfThreads)
    {
        Threads.push_back(std::thread(IncrementValue, nThreadIndex++));
    }
    for(auto &Thread : Threads)
    {
        Thread.join();
    }
    return EXIT_SUCCESS;
}

However running this under VS2013 in debug build (so no optimizations), fails to produce any errors - the break-point is not hit and the threads do not exit. Is my example inaccurate or is there something that prevents this from exposing false sharing on a single physical CPU?

0

There are 0 best solutions below