2 ms·
A minimal program (using C11 threads): #include <threads.h> int x; int do_thread(void *) { for (int i = 0; i < 10000; i++) x = i; re
by jcranmer 7y ago
A minimal program (using C11 threads):
#include <threads.h>
int x;
int do_thread(void *) {
for (int i = 0; i < 10000; i++)
x = i;
return 0;
}
int main() {
thrd_t t;
thrd_create(&t, do_thread, NULL);
do_thread(NULL);
thrd_join(&t, NULL);
return 0;
}
There is an undefined data race on x, since it is simultaneously accessed from two threads without any synchronization. Replace the declaration of x with `_Atomic int x;`, and the resulting code is well-defined.