35 lines
753 B
C
35 lines
753 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
|
|
volatile long long counter = 0;
|
|
long long niters;
|
|
|
|
void *thread_func(void *arg) {
|
|
for (long long i = 0; i < niters; i++) {
|
|
counter++;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage: %s <niters>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
niters = atoll(argv[1]);
|
|
|
|
pthread_t t1, t2;
|
|
pthread_create(&t1, NULL, thread_func, NULL);
|
|
pthread_create(&t2, NULL, thread_func, NULL);
|
|
|
|
pthread_join(t1, NULL);
|
|
pthread_join(t2, NULL);
|
|
|
|
printf("Expected: %lld, Got: %lld\n", 2 * niters, counter);
|
|
if (counter != 2 * niters) {
|
|
printf("ERROR: Race condition detected!\n");
|
|
}
|
|
return 0;
|
|
}
|