c - does read / write atomicity hold on multi-computer memory? -


i'm working in multi-computer environment 2 computers access same memory on 32bit pci bus.

the first computer write 32bit int.
*int_pointer = number;

the second computer read 32bit int.
number = *int_pointer;

both os's / cpu's 32bit architecture.
computer pci intel based.
computer on pci card power pc.

the case worried if write computer changing variable @ same time read computer reading it, causing invalid data read computer.
i'd know if atomicity of reading / writing same position in memory preserved on multiple computers.

if so, following prevent race condition:

number = *int_pointer; while(*int_pointer != number) {     number = *int_pointer; } 

i can guarantee writes occur every 16ms*, , reads occur randomly.

*times drift since both computers have different timers.

there isn't enough information answer definitively.

if either of cpus decide cache memory @ *int_pointer, fail , code not fix race condition.

assuming both cpus know memory @ *int_pointer uncacheable, and location *int_pointer aligned on 4-byte/32-bit boundary and cpu on pci card has 32 bit memory interface, and pointer declared pointer volatile and both c compilers implement volatile correctly, updates atomic.

if of above conditions not met, result unpredictable , "race detection" code not work.

edited explain why volatile needed:

here race condition code compiled mips assembler -o4 , no volatile qualifier. (i used mips because generated code easier read x86 code):

int nonvol(int *ip) {   int number = *ip;   while (*ip != number) {     number = *ip;   }   return number; } 

disassembly output:

        00000000 <nonvol>:    0:   8c820000        lw      v0,0(a0)    4:   03e00008        jr      ra 

the compiler has optimized while loop away since knows *ip cannot change.

here's happens volatile , same compiler options:

int vol(volatile int *ip) {   int number = *ip;   while (*ip != number) {     number = *ip;   }   return number; } 

disassembly output:

        00000008 <vol>:    8:   8c820000        lw      v0,0(a0)    c:   8c830000        lw      v1,0(a0)   10:   1443fffd        bne     v0,v1,8 <vol>   14:   00000000        nop   18:   03e00008        jr      ra 

now while loop not optimized away because using volatile has told compiler *ip can change @ time.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -