Consider the following multi threaded code.
volatile static int flag1= 0, flag2= 0;
// code for thread 1 (or T1)
flag1 = 1;
if (flag2==0)
code1();
// end of code for T1
// code for thread 2 (or T2)
flag2 = 1;
if (flag1 == 0)
code2();
// end of code for T2
Which of the following is true ?
What can happen on a mono-processor machine?
- code1() is invoked, but not code2().
- code2() is invoke, but not code1().
- no method is invoked.
- both methods cannot be invoked!
Also, what will be the answer if we remove the volatile type from the flags.