Skip to content

Instantly share code, notes, and snippets.

@ato
Created October 8, 2010 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ato/616670 to your computer and use it in GitHub Desktop.
Save ato/616670 to your computer and use it in GitHub Desktop.
// 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