multithreading - Two more more threads writing the same value to the same memory location -
i have situation several threads write same value same memory location.
can lead memory location storing corrupt value resulting concurrent writes ?
let's have object of class unique id. when used threads, these threads assign id them, 100. question : can id value other 100 after of threads write 100 memory location? in other words, have protect id mutex ?
i think multiple non-atomic writes of same value guaranteed safe (i.e. producing same result 1 write) if these 2 conditions hold:
- a non-atomic write constructed series of atomic writes
- several atomic writes of same value location produce same value
both of these seem natural enough expect, not sure true every possible implementation.
the example thinking of following:
suppose 2 processes write 2-byte value 1
address a
. value written 2 separate atomic bytes: 1
address a
, , 0
address a+1
. if have 2 processes (p
,q
), both writing first value 1
address (say) 10
, writing value 0
address 11
, without mutual exclusion following possible executions:
p[1->10]
,p[0->11]
,q[1->10]
,q[0->11]
p[1->10]
,q[1->10]
,p[0->11]
,q[0->11]
p[1->10]
,q[1->10]
,q[0->11]
,p[0->11]
q[1->10]
,q[0->11]
,p[1->10]
,p[0->11]
q[1->10]
,p[1->10]
,q[0->11]
,p[0->11]
q[1->10]
,p[1->10]
,p[0->11]
,q[0->11]
either way write 1
twice location 10
, , write 0
twice location 11
atomically. if 2 writes produce same result 1 write, either of above sequences produces same result.
Comments
Post a Comment