mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user