title: How Can synchronize data between differernt cores on Xeon (linux how to use memory barriers)
body: <p>I wrote a simple program to test memory Synchronization. Use a global queue to share with two
  processes, and bind two processes to different cores. my code is blew.</p>
  
  <pre><code> #include&lt;stdio.h&gt;
   #include&lt;sched.h&gt;
   #define __USE_GNU
  
   void bindcpu(int pid) {
       int cpuid;
       cpu_set_t mask;
       cpu_set_t get;
       CPU_ZERO(&amp;mask);
  
       if (pid &gt; 0) {
           cpuid = 1;
       } else {
           cpuid = 5;
       }
  
       CPU_SET(cpuid, &amp;mask);
  
       if (sched_setaffinity(0, sizeof(mask), &amp;mask) == -1) {
           printf("warning: could not set CPU affinity, continuing...\n");
       }
   }
  
   #define Q_LENGTH 512
   int g_queue[512];
  
   struct point {
       int volatile w;
       int volatile r;
   };  
  
   volatile struct point g_p;
  
  
   void iwrite(int x) {
       while (g_p.r == g_p.w);
       sleep(0.1);
       g_queue[g_p.w] = x;
       g_p.w = (g_p.w + 1) % Q_LENGTH;
       printf("#%d!%d", g_p.w, g_p.r);
   }
  
   void iread(int *x) {
       while (((g_p.r + 1) % Q_LENGTH) == g_p.w);
       *x = g_queue[g_p.r];
       g_p.r = (g_p.r + 1) % Q_LENGTH;
       printf("-%d*%d", g_p.r, g_p.w);
   }
  
   int main(int argc, char * argv[]) {
       //int num = sysconf(_SC_NPROCESSORS_CONF);
       int pid;
  
       pid = fork();
       g_p.r = Q_LENGTH;
       bindcpu(pid);
       int i = 0, j = 0;
  
       if (pid &gt; 0) {
           printf("call iwrite \0");
           while (1) {
               iread(&amp;j);
           }
       } else {
           printf("call iread\0");
           while (1) {
               iwrite(i);
               i++;
           }
       }
  
   }
  </code></pre>
  
  <p>The data between the two processesIntel(R) Xeon(R) CPU E3-1230 and two cores didn't synchronized.</p>
  
  <p>CPU: Intel(R) Xeon(R) CPU E3-1230
  OS: 3.8.0-35-generic #50~precise1-Ubuntu SMP</p>
  
  <p>I want to know beyond IPC How I can synchronize the data between the different cores in user
   space ?</p>