Thursday, April 21, 2005

This will THREAD your brains



An interesting question...

Consider a function PlusPlus()

static int i;

void PlusPlus()
{
i++;
}

Now 2 threads are created...

THREAD 1
PlusPlus();
PlusPlus();
PlusPlus();

THREAD 2
PlusPlus();
PlusPlus();
PlusPlus();
PlusPlus();
PlusPlus();

Assuming that all necessary work is done to set up these threads & PlusPlus function, what will be the MAX & MIN value of "i" after the threads stop executing. Support your answers with some explanations.

ASSUMPTIONS
1. The system scheduler follows random scheduling algorithm
2. Thread 1 or Thread 2, either can be executed first (because of 1st assumpt.)

6 comments:

Anonymous said...

minimum ---- 0

maximum ----8

waise when u r going to give answer of this????

Manuj said...

maku courier kiya kya

makuchaku said...

Reading about threads...
Will try this problem 2moro & post the results here.

As of now, even i cannot tell the answer.

Manuj said...

maku courier kar de na
plssssssssssssssssssss

harpreet said...

i think its :
min. 3 and max. 8.......

makuchaku said...

I gave same answer as HP.
Lets see whats the actual answer :-)