Quali istruzioni cede atomicità in questa espressione che rende il risultato 2?
-
16-10-2019 - |
Domanda
Sto leggendo l'atomicità e mi sono imbattuto il seguente scenario
int x = y = z = 0;
Thread 1 Thread 2
--------- --------
x = y + z y = 1
z = 2
che fornisce i seguenti gruppi di uscita ??p>
$$ \ begin {array} {} ccc 1 & 2 & 3 \\ T1: x = y + z & T2: y = 1 & T2: y = 1 \\ T2: y = 1 & T2: z = 2 & T1: x = y + z \\ T2: z = 2 & T1: x = y + z & T2: z = 2 \ end {array} $$
Traducendo la $ x = y + z $ espressione in codice macchina dà
load r1; y
load r2; z
add r3; r1; r2
store r3;
Tuttavia secondo alcune note che ho letto scendendo il sentiero della
T1 : load r1, y
T2 : y = 1
z = 2
T1 : load r2, z
add r3, r1, r
store r3, x
Non riesco a capire come l'autore è venuto a risultato che $ x = 2 $.
Per me, sulla base delle istruzioni macchina precedenti il ??risultato dovrebbe essere di 3, che credo mi porta a rendersi conto che dovrei colpo Eureka (o di una semplice lettura errata) e rendersi conto in cui si verifica l'atomicità . Potrebbe spiegare l'atomicità in questa abbastanza semplice dichiarazione che porta al risultato corretto?
Soluzione
The point you are missing is that in thread $y$ is loaded into $r1$ and that any subsequent changes to $y$ will not affect the value stored in $r1$. Thus, the value in $r1$ is $0$ even after statement $y=1$. From this it is easy to see that the result is $x=2$.