JMM - is this program data race free?

86 views
Skip to first unread message

r r

unread,
Oct 23, 2022, 12:59:41 PM10/23/22
to mechanical-sympathy
int x;
volatile int v;

write(x, 1)     read(v)        
write(v, 1)     read(x) 


Is that program data race free? On my eye it is not not because there is an execution that write(x, 1) and read(x) are not in happens-before relationship.

Peter Veentjer

unread,
Oct 23, 2022, 11:09:01 PM10/23/22
to mechanica...@googlegroups.com
The program isn't clear to me. I guess you want something like this:

int x;
volatile int v;

CPU1:
    write(x, 1)       (1)
    write(v, 1)        (2)

CPU2
    read(v)         (3)
    read(x)        (4)

Then there exists an execution where (4) sees value written by (1) it is in a data race with. E.g. (1)->(3)->(4).

So since the program has at least 1 execution with a data race. So your observation that the program is not data race free, is correct.

To resolve the data race, you want to do something like this:

int x;
volatile int v;

CPU1:
    write(x, 1)       (1)
    write(v, 1)       (2)

CPU2
    if(read(v)==1){ (3)
        print(read(x));  (4)
    }

When (3) has observed the value written by (2) there is a happens-before edge between (2) and (3). And due to the program order between (1)/(2) and (3)/(4) and the transitive nature of happens-before, there is a happens-before edge between (1) and (4). So the value'1' should be printed.

This program is data race free.




--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/9a25a145-5271-4f7a-ad3d-3633367becd4n%40googlegroups.com.

r r

unread,
Oct 24, 2022, 2:38:07 AM10/24/22
to mechanica...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages