Created
October 8, 2010 11:53
-
-
Save ato/616670 to your computer and use it in GitHub Desktop.
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
// Compiled with gcc -O | |
/*** Not locking ***/ | |
#include <pthread.h> | |
#include <malloc.h> | |
int main() { | |
int i; | |
char *ba = malloc(100000000); | |
for (i = 0; i < 80000000; i++) { | |
ba[i] = 0; | |
} | |
// this is to stop the sneaky compiler from optimizing the whole lot away ;-) | |
printf("%d\n", ba[70000000]); | |
} | |
// ./bat 0.05s user 0.03s system 97% cpu 0.082 total | |
/*** Locking ***/ | |
#include <pthread.h> | |
#include <malloc.h> | |
int main() { | |
pthread_mutex_t mutex; | |
int i; | |
char *ba = malloc(100000000); | |
for (i = 0; i < 80000000; i++) { | |
pthread_mutex_lock(&mutex); | |
ba[i] = 0; | |
pthread_mutex_unlock(&mutex); | |
} | |
// this is to stop the sneaky compiler from optimizing the whole lot away ;-) | |
printf("%d\n", ba[70000000]); | |
} | |
// ./bat-lock 0.33s user 0.02s system 99% cpu 0.356 total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment