Previous | Next --- Slide 50 of 90
Back to Lecture Thumbnails
lonelymoon

In this case, thread 2 intervenes the process in the tread 1. If the initialization of the tread 2 happens at the midst of the for loop in the thread 1, the X will be overwritten as zero and makes different print from the expected result.

haiyuem

This is an example that programming language optimizations might cause program output inconsistency.

weimin

The left code fragment is the original code before compiler optimization and if threads 1 and 2 execute concurrently, it would print series of 1s and then 0 due to the write from thread 2 and due to the X=1 in the loop continue printing 1 until iteration 100. The right code fragment has the X=1 optimized out of the loop by the compiler so now when thread 2 writes to X the loop will print 0 for every iteration after that.

blipblop

This example program is not properly synchronized and contains a data race. Since the compiler only guarantees "SC for DRF", and makes no guarantees for data-race-full programs, it is OK for the program output to depend on optimization choices made by the compiler, as happens in this example.

Please log in to leave a comment.