Skip to content

Commit e04392d

Browse files
committed
Align tex file with updated code example
The compile command is added in the Makefile, same as the previous example code. Missing reference and wrong compiler flag are fixed as well.
1 parent ecd1b57 commit e04392d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

concurrency-primer.tex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,6 @@ \subsection{ABA problem}
893893

894894
\inputminted{c}{./examples/simple_aba_example.c}
895895

896-
Compile it with \monobox{gcc -std=c11 -Wall -Wextra -pthread simple\_aba\_example.c}.
897896
The execution result would be:
898897

899898
\begin{ccode}
@@ -916,9 +915,10 @@ \subsection{ABA problem}
916915

917916
\begin{ccode}
918917
job_t *job = atomic_load(&thrd_pool->head->prev);
918+
...
919919
while (!atomic_compare_exchange_weak(&thrd_pool->head->prev, &job,
920-
job->prev)) {
921-
}
920+
job->prev))
921+
;
922922
\end{ccode}
923923

924924
Consider the following scenario:
@@ -939,7 +939,7 @@ \subsection{ABA problem}
939939
At the end, the dangling pointer could either point to garbage or trigger segmentation fault.
940940
It could be even worse if nested ABA problem occurs in thread B.
941941
Also, the possibility to allocate a job with same address could be higher when using memory pool, meaning that more chances to have ABA problem occurred.
942-
In fact, pre-allocated memory should be used to achive lock-free since \monobox{malloc} could have mutex involved in multithreaded environment.
942+
In fact, pre-allocated memory should be used to achieve lock-free since \monobox{malloc} could have mutex involved in multithreaded environment.
943943

944944
Failure to recognize changed target object through comparison can result in stale information.
945945
The general concept of solving this problem involves adding more information to make different state distinguishable, and then making a decision on whether to act on the old state or retry with the new state.
@@ -957,8 +957,8 @@ \subsection{ABA problem}
957957
Sometimes, this is referred to as \introduce{double-width compare-and-swap}.
958958
On x86-64 processors, for atomic instructions that load or store more than a CPU word size, it needs additional hardware support.
959959
You can use \monobox{\$ grep cx16 /proc/cpuinfo} to check if the processor supports 16-byte compare-and-swap.
960-
For hardware that does not support the desired size, software implementations which may have locks involve are used instead as mentioned in \secref{arbitrarily-size}.
961-
Back to the example, ABA problem in the following code is fixed by using an version number that increments each time a job is added to the empty queue. On x86-64, add a compiler flag \monobox{-mcx64} to enable 16-byte compare-and-swap in \monobox{worker} function.
960+
For hardware that does not support the desired size, software implementations which may have locks involve are used instead, as mentioned in \secref{atomictype}.
961+
Back to the example, ABA problem in the following code is fixed by using an version number that increments each time a job is added to the empty queue. On x86-64, add a compiler flag \monobox{-mcx16} to enable 16-byte compare-and-swap in \monobox{worker} function.
962962

963963
\inputminted{c}{./examples/rmw_example_aba.c}
964964

examples/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
all:
22
$(CC) -Wall -o rmw_example rmw_example.c -pthread -lm
33
$(CC) -Wall -o rmw_example_aba rmw_example_aba.c -pthread -lm -mcx16
4+
$(CC) -Wall -o simple_aba_example simple_aba_example.c -pthread
45
clean:
5-
rm -f rmw_example rmw_example_aba
6+
rm -f rmw_example rmw_example_aba simple_aba_example
67
check: all
78
./rmw_example
89
./rmw_example_aba
10+
./simple_aba_example

0 commit comments

Comments
 (0)