Skip to content

More fun with threading

Here’s an interesting post from benwu.
After executing Thread1 and Thread2, what are the possible values for “i”?

bool b1 = false;
bool b2 = false;
int i = 0;
private void Thread1()
{
    b1 = true;
    if (b2)
    {
        i = 1;
    }
}
private void Thread2()
{
    b2 = true;
    if (b1)
    {
        i = 1;
    }
}

On multi-processor machines, sometimes it will be zero. Check here for his explanation.