Created
June 18, 2013 20:51
-
-
Save isaiah-perumalla/5809246 to your computer and use it in GitHub Desktop.
x86 memory model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdio.h> | |
int x, y = 0; | |
int r0, r1; | |
void *core0 (void *arg) | |
{ | |
x = 1; | |
asm volatile ("" ::: "memory"); // ensure GCC compiler will not reorder | |
r1 = y; | |
return 0; | |
} | |
void *core1 (void *arg) | |
{ | |
y = 1; | |
asm volatile ("" ::: "memory"); // ensure GCC compiler will not reorder | |
r0 = x; | |
return 0; | |
} | |
int main (void) | |
{ | |
pthread_t thread0, thread1; | |
while (1) { | |
x = y = 0; | |
//Start threads | |
pthread_create (&thread0, NULL, core0, NULL); | |
pthread_create (&thread1, NULL, core1, NULL); | |
//wait for threads to complete | |
pthread_join (thread0, NULL); | |
pthread_join (thread1, NULL); | |
if (r0 == 0 && r1 ==0) { | |
printf ("(r0=%d, r1=%d)\n", r0, r1); | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment