Last active
December 18, 2015 17:08
-
-
Save isaiah-perumalla/5815957 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ("mfence" ::: "memory"); // ensure compiler or hardware will not reorder, store buffer is drained | |
r1 = y; | |
return 0; | |
} | |
void *core1 (void *arg) | |
{ | |
y = 1; | |
asm volatile ("mfence" ::: "memory"); // ensure compiler or hardware will not reorder, store buffer is drained | |
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