c++ - __sync_synchronize() not working as expected? -


consider following scenario:

requirements:

  • intel x64 server (multiple cpu-sockets => numa)
  • ubuntu 12, gcc 4.8.1, mpich-3
  • two processes sharing data on shared-memory

program sequence (pseudo code):

program:

volatile int data = 0; //allocated in (named) shared-memory volatile int flag = 0; //allocated in (named) shared-memory  void setdata() {     data = 1;       __sync_synchronize();     flag = 1;   }  void getdata( int& result ) {     //wait     while( !flag ){};      //data ready     result = data; } 

process a:

int main( int argc, char** argv ) {     setdata();     ::std::cin.ignore();      return 0; } 

process b:

int main( int argc, char** argv ) {        int result = 0;     getdata();      //print result (result should 1)     ::std::cout << "received: " << result << ::std::endl;      return 0; } 

when run program, expect "result" 1 @ end of process b. while "waiting loop" in getdata() works fine, result in cases "wrong" (result=0).

i can throw in memory barrier in getdata() , compiler memory barriers (asm volatile ( "" ::: "memory" )), results still remain erratic.

so question is, shouldn't memory barrier (__sync_synchronize) in setdata() guarantee previous reads/writes have been comitted memory, before operation after it?

ps: follow question to: shared-memory ipc synchronization (lock-free)

edit (probably important): use mpich-3 start processes. render every assumption make memory visibility/consistency invalid?


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -