Is a security risk of primitive type threads using only bloat or tune?

Last week, I already worked a little on the warpath, about the fact that Cortex-A9 can be obtained and installed from multiple streams on two-lane courts without a hint of thread safety. It seems to be working fine, and after a bit of testing, I can't get my desktop to skip a simple test using __int128. Are the chances of reading the wrong value nonzero? If the chances are significant and I'm not doing it right?

Any sample code that fails incorrectly on a modern x86_64 machine will be great. But only using get / set on primitive types.

+4
source share
1 answer

:

#include <iostream>
#include <thread>
#include <array>

std::array<int, 10> sharedArray;
void taskOne()
{
    while (true)
    {
        for (int i = 0; i < sharedArray.size(); ++i)
        {
            sharedArray[i] = 5;
        }

        for (int i = 0; i < sharedArray.size(); ++i)
        {
            if (sharedArray[i] != 5)
            {
                std::cout << "ERROR";
                std::terminate();
            }
        }
    }
}

void taskTwo()
{
    while (true)
    {
        for (int i = 0; i < sharedArray.size(); ++i)
        {
            sharedArray[i] = 10;
        }
    }
}

int main()
{
    std::thread t1{ taskOne };
    std::thread t2{ taskTwo };
    t1.join();
    t2.join();
}

ERROR x86_64 MSVC. ( Ideone .)

, , , .

+3

Source: https://habr.com/ru/post/1693853/


All Articles