Showing posts with label Threads. Show all posts
Showing posts with label Threads. Show all posts

19 October 2011

Threads Question

I heard a question this week, and I thought it would be nice sharing it here, I did solve it, however, I'm not 100% sure I did it right :), so here it is:
What is the maximum value the variable i can get in each case below, assuming that it's a global int initialized by zero (int i=0) and the code in each case is running on 2 threads?

case1:
while(i < 1000)
{
    i++;
}
case2:
while(i < 1000)
{
    ++i;
}

case3:
while(i++ < 1000);

case4:
while(++i < 1000);