From f35f10856741fd0d3168a970cbee91542385145f Mon Sep 17 00:00:00 2001 From: "YU-WEI,HSU" Date: Wed, 11 Jun 2025 23:05:19 +0800 Subject: [PATCH 1/2] Note -latomic needed when using -mcx16 The ABA example relies on 16-byte atomic compare-and-swap operations, which require the `-mcx16` compiler flag on x86-64 to enable the corresponding instructions. However, due to current limitations in GCC, 16-byte atomic operations (e.g., `__atomic_load_16` and `__atomic_compare_exchange_16`) are implemented via calls to `libatomic` on many platforms. This results in linker errors unless `-latomic` is explicitly added. To avoid confusion and ensure portability, this patch adds a footnote to the LaTeX document explaining that `-latomic` must be linked when using `-mcx16`. For more details, see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104688 --- concurrency-primer.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concurrency-primer.tex b/concurrency-primer.tex index d5c0400..3b5d36e 100644 --- a/concurrency-primer.tex +++ b/concurrency-primer.tex @@ -959,7 +959,7 @@ \subsection{ABA problem} On x86-64 processors, for atomic instructions that load or store more than a CPU word size, it needs additional hardware support. You can use \monobox{grep cx16 /proc/cpuinfo} to check if the processor supports 16-byte compare-and-swap. For hardware that does not support the desired size, software implementations which may have locks involve are used instead, as mentioned in \secref{atomictype}. -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. +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.\footnote{When using \texttt{-mcx16}, programs relying on 16-byte atomic operations (e.g., using \texttt{\_\_int128}) may require linking with \texttt{-latomic}, as these operations are implemented via function calls to \texttt{libatomic} on some systems. See \url{https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104688}.} \inputminted{c}{./examples/rmw_example_aba.c} From 1a4d1f78c69855cdb622edebe1f180f55069c918 Mon Sep 17 00:00:00 2001 From: "YU-WEI,HSU" Date: Wed, 11 Jun 2025 23:30:37 +0800 Subject: [PATCH 2/2] Fix function name in book: thread_pool_destroy Page 12 refers to `thread_pool_destroy`, but the actual function name used in the example code is `tpool_destroy`. This commit corrects the discrepancy to avoid confusion. --- concurrency-primer.tex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/concurrency-primer.tex b/concurrency-primer.tex index 3b5d36e..81b5f90 100644 --- a/concurrency-primer.tex +++ b/concurrency-primer.tex @@ -529,7 +529,7 @@ \subsection{example} \end{ccode} \textbf{Exchange} -In function \monobox{thread\_pool\_destroy}, \monobox{atomic\_exchange(\&thrd\_pool->state, cancelled)} reads the current state and replaces it with ``cancelled''. +In function \monobox{tpool\_destroy}, \monobox{atomic\_exchange(\&thrd\_pool->state, cancelled)} reads the current state and replaces it with ``cancelled''. A warning message is printed if the pool is destroyed while workers are still ``running''. If the exchange is not performed atomically, we may initially get the state as ``running''. Subsequently, a thread could set the state to ``cancelled'' after finishing the last one, resulting in a false warning. @@ -541,7 +541,7 @@ \subsection{example} This indicates that the main thread will wail until the worker completes the job and returns ownership back to the main thread, which ensures correct cooperation. \textbf{Fetch and…} -In the function \monobox{thread\_pool\_destroy}, \monobox{atomic\_fetch\_and} is utilized as a means to set the state to ``idle''. +In the function \monobox{tpool\_destroy}, \monobox{atomic\_fetch\_and} is utilized as a means to set the state to ``idle''. Yet, in this case, it is not necessary, as the pool needs to be reinitialized for further use regardless. Its return value could be further utilized, for instance, to report the previous state and perform additional actions.