Each of the two lines in your question is meant to represent a separate thread. Since the shared variable x
is not guarded, pretty much anything can happen. What are the possibilities you came up with? (otherwise I would feel like I'm just answering your problem for you)
SECOND HINT: Let a C compiler help you by showing you some examples...
int x;
void f(void)
{
int i;
for (i = 0; i < 5; i++) x = x + 1;
}
(this is your program translated into C)
gcc -S -fno-PIC t.c
(this is what I have to type on my machine to get readable assembly)
_f:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $0, -12(%ebp)
jmp L2
L3:
movl _x, %eax
incl %eax
movl %eax, _x
leal -12(%ebp), %eax
incl (%eax)
L2:
cmpl $4, -12(%ebp)
jle L3
leave
ret
Now with optimizations (adding option -O2 to the compilation):
_f:
movl _x, %eax
xorl %edx, %edx
pushl %ebp
movl %esp, %ebp
.align 4,0x90
L2:
incl %edx
incl %eax
cmpl $5, %edx
jne L2
movl %eax, _x
leave
ret