Files
linux-kernel-module-cheat/userland/c/atomic.c
Ciro Santilli 六四事件 法轮功 0d5c7f5c4c Detailed gem5 analysis of how data races happen
And pass niters as a thread argument to all threading implementations...
otherwise every loop has to do a memory load from the global!
2020-06-05 06:00:05 +00:00

50 lines
1.1 KiB
C

/* https://cirosantilli.com/linux-kernel-module-cheat#atomic-c */
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>
#include <string.h>
#include <stdlib.h>
atomic_int acnt;
int cnt;
int my_thread_main(void *thr_data) {
size_t niters = *(size_t *)thr_data;
for (size_t i = 0; i < niters; ++i) {
++cnt;
++acnt;
}
return 0;
}
#endif
int main(int argc, char **argv) {
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
size_t niters, nthreads;
thrd_t *threads;
if (argc > 1) {
nthreads = strtoull(argv[1], NULL, 0);
} else {
nthreads = 2;
}
if (argc > 2) {
niters = strtoull(argv[2], NULL, 0);
} else {
niters = 10;
}
threads = malloc(sizeof(thrd_t) * nthreads);
for(size_t i = 0; i < nthreads; ++i)
thrd_create(threads + i, my_thread_main, &niters);
for(size_t i = 0; i < nthreads; ++i)
thrd_join(threads[i], NULL);
free(threads);
printf("atomic %u\n", acnt);
printf("non-atomic %u\n", cnt);
#else
(void)argc;
(void)argv;
#endif
}