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!
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-06-05 06:00:05 +00:00
parent 619fef4b04
commit 0d5c7f5c4c
6 changed files with 162 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#c-multithreading */
/* https://cirosantilli.com/linux-kernel-module-cheat#atomic-c */
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
#include <stdatomic.h>
@@ -9,10 +9,9 @@
atomic_int acnt;
int cnt;
size_t niters;
int f(void *thr_data) {
(void)thr_data;
int my_thread_main(void *thr_data) {
size_t niters = *(size_t *)thr_data;
for (size_t i = 0; i < niters; ++i) {
++cnt;
++acnt;
@@ -23,7 +22,7 @@ int f(void *thr_data) {
int main(int argc, char **argv) {
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
size_t nthreads;
size_t niters, nthreads;
thrd_t *threads;
if (argc > 1) {
nthreads = strtoull(argv[1], NULL, 0);
@@ -37,7 +36,7 @@ int main(int argc, char **argv) {
}
threads = malloc(sizeof(thrd_t) * nthreads);
for(size_t i = 0; i < nthreads; ++i)
thrd_create(threads + i, f, NULL);
thrd_create(threads + i, my_thread_main, &niters);
for(size_t i = 0; i < nthreads; ++i)
thrd_join(threads[i], NULL);
free(threads);