Skip to content

Instantly share code, notes, and snippets.

@philippkeller
Created December 29, 2017 17:15
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 philippkeller/98600abe00030e3e6e5791ce1b50f583 to your computer and use it in GitHub Desktop.
Save philippkeller/98600abe00030e3e6e5791ce1b50f583 to your computer and use it in GitHub Desktop.
Test thread data races
min=40000
for i in {1..10000}; do
a=$(./test)
min=$((a<min?a:min));
done
echo $min
#include <pthread.h>
#include <stdio.h>
int var = 0;
#define NTHREADS 2
#define MAXVAL 40000 / NTHREADS
void *other_thread() {
for(int i=0; i<MAXVAL; i++) {
var++;
}
}
int main() {
pthread_t threads[NTHREADS];
for (int i=0;i<NTHREADS;i++) {
pthread_create(&threads[i], NULL, other_thread, NULL);
}
for (int i=0;i<NTHREADS;i++) {
pthread_join(threads[i], NULL);
}
printf("%d\n", var);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment