3 ms·
Thread A sets g1 to A (line 31), thread A enters the "if" statement (line 32), thread A reads the value of g1 and is about to write that value to g2 but gets pr
by wzdd 12y ago
Thread A sets g1 to A (line 31), thread A enters the "if" statement (line 32), thread A reads the value of g1 and is about to write that value to g2 but gets pre-empted (half of line 33).
Thread B sets g1 to B (line 31), thread B enters the "if" statement (because A hasn't yet written g1 to g2, line 32), thread B reads the value of g1 (half of line 33) and writes that value to g2 (the other half), then does its tests and returns OK.
Thread A sets g2 to A (the other half of line 33). Thread A does its tests and notices that g1 != g2. Thread A returns to the top of the loop and notices that g2 == A (line 22). Thread A returns OK.
This is assuming that reads are strongly ordered between processes and cores, and that code executes in-order, none of which is true.