Created
December 29, 2017 17:15
-
-
Save philippkeller/98600abe00030e3e6e5791ce1b50f583 to your computer and use it in GitHub Desktop.
Test thread data races
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
min=40000 | |
for i in {1..10000}; do | |
a=$(./test) | |
min=$((a<min?a:min)); | |
done | |
echo $min |
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 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