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
Copy file name to clipboardExpand all lines: concurrency-primer.tex
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -893,7 +893,6 @@ \subsection{ABA problem}
893
893
894
894
\inputminted{c}{./examples/simple_aba_example.c}
895
895
896
-
Compile it with \monobox{gcc -std=c11 -Wall -Wextra -pthread simple\_aba\_example.c}.
897
896
The execution result would be:
898
897
899
898
\begin{ccode}
@@ -916,9 +915,10 @@ \subsection{ABA problem}
916
915
917
916
\begin{ccode}
918
917
job_t *job = atomic_load(&thrd_pool->head->prev);
918
+
...
919
919
while (!atomic_compare_exchange_weak(&thrd_pool->head->prev, &job,
920
-
job->prev)) {
921
-
}
920
+
job->prev))
921
+
;
922
922
\end{ccode}
923
923
924
924
Consider the following scenario:
@@ -939,7 +939,7 @@ \subsection{ABA problem}
939
939
At the end, the dangling pointer could either point to garbage or trigger segmentation fault.
940
940
It could be even worse if nested ABA problem occurs in thread B.
941
941
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.
943
943
944
944
Failure to recognize changed target object through comparison can result in stale information.
945
945
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}
957
957
Sometimes, this is referred to as \introduce{double-width compare-and-swap}.
958
958
On x86-64 processors, for atomic instructions that load or store more than a CPU word size, it needs additional hardware support.
959
959
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.
0 commit comments