You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introducing thread sanitizer here may be an unexpected pop up for the
readers that are new to concurrency. Here we focus on rmw atomic
operation instead, thus related content and diff file are removed.
The proper place for this topic could be a dedicated section for
"testing, debugging and verifing concurrent programs"
This aligns the decision sticking to C11 thread as well.
Co-authored-by: Wei-Hsin Yeh <[email protected]>
Copy file name to clipboardExpand all lines: concurrency-primer.tex
+6-22Lines changed: 6 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -550,28 +550,12 @@ \subsection{example}
550
550
They behave equivalently to a \cc|do while| loop. See \clang{}11 standard 6.5.2.4 and 6.5.16.2 for more details.
551
551
552
552
What if claiming a job, which updates \cc|thrd_pool->head->prev|, is not done atomically?
553
-
Let's remove the atomicity of claiming a job and use thread sanitizer to detect races.
554
-
Thread sanitizer is one of the sanitizers provided by \introduce{gcc} and \introduce{clang} to detect data races.
555
-
Data races are undefined behavior in \clang{}11 and \cplusplus{}11.
556
-
Thread sanitizer inserts runtime code into the target program to track memory accesses.
557
-
When races occur during execution, warning messages are printed.
558
-
To enable this, add \monobox{-fsanitize=thread -g} to compiler flags.
559
-
The following diff patch removes the atomicity of claiming a job and uses pthread instead of \clang{}11 thread, because thread sanitizer currently hasn't support \clang{}11 thread yet.
560
-
Save diff as \monobox{racer.diff} and patch the example code by \monobox{\$ patch rmw\_example.c race.diff}.
561
-
562
-
%\inputminted{diff}{./examples/racer.diff}
563
-
564
-
After compiling and running the example, you will see warning messages printed and same job IDs got echoed repeatly.
565
-
The top two sections of a warning message indicate which two threads executed which function causing the data race.
566
-
The bottom two sections indicate how these two threads were created.
567
-
If the race occurred on a heap block, a third section would appear indicating how the block was allocated.
568
-
At the end of a warning message, a summary indicates the type of race and where it occurred.
569
-
You'll see that two lines of code, which claim a job, are highlighted as causing a data race on \cc|thrd_pool->head|.
570
-
571
-
You may notice that there is another part of the code causing races.
572
-
While some workers were echoing ID, others were attempting to free the job, resulting in heap-use-after-free and data races.
573
-
This occurred when two workers claimed the same job, as the claiming process was not atomic.
574
-
But even when jobs were claimed atomically, this still can occur.
553
+
Two or more threads could have races updating \cc|thrd_pool->head->prev| and working on the same job.
554
+
Data races are undefined behavior in \clang{}11 and \cplusplus{}11.
555
+
Working on the same job can lead to duplication of the calculation of \cc|job->future->result|,
556
+
use after free and double free on the job.
557
+
558
+
But even when jobs were claimed atomically, a thread can still have chances holding a job that has been freed.
575
559
This is a defect of the example code.
576
560
Jobs in the example are dynamically allocated. They are freed after worker finishes each job.
577
561
However, this situation may lead to dangling pointers for workers that are still holding and attempting to claim the job.
0 commit comments