Skip to content

Commit 135134f

Browse files
committed
Rewording in threads.
1 parent 7ef6861 commit 135134f

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

threads.tex

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,30 +1259,28 @@
12591259
\item \label{ATOMIC_ADD} Example \example{race/atomic-add.c} demonstrates
12601260
the race condition problem when performing addition and its possible solutions.
12611261
The program spawns two threads, each thread works with the same global variable
1262-
\emsl{x}, into which it adds the numbers from 1 to specified value
1263-
\emsl{\texttt{arg}}.
1264-
The threads run in parallel, each performs this cycle:
1262+
\emsl{x}, and in a loop, each increments the variable by numbers from a sequence
1263+
of 1 to \emsl{\texttt{arg[1]}}. The threads run in parallel, they compete for
1264+
access to \texttt{x}, and each performs the following cycle:
12651265

12661266
\begin{verbatim}
12671267
for (i = 1; i < arg; ++i)
12681268
x = x + i;
12691269
\end{verbatim}
12701270

1271-
After the cycle the main thread performs check and the double (there were
1272-
2 threads) compares with the value of the \emsl{x} global variable.
1273-
If the results are not identical, race condition was hit (we can ignore
1274-
integer overflow in this case).
1275-
\par
1276-
The results and times to completion are radically different for situations
1277-
when the program used plain addition, atomic arithmetic functions and locking
1278-
using mutexes. There is difference in completion times between
1279-
arithmetic operation functions and mutexes is especially notable on
1280-
CPUs with hardware parallelism support.
1281-
\item Similarly there is API for subtraction, bit operations
1282-
AND and OR, value assignment etc.
1283-
\item The atomic primitives and types in C11 is optional feature, so should
1271+
After the cycle the main thread performs a check of the resulting value of
1272+
\texttt{x}. If the result is not a double of a sum of the sequence, a race
1273+
condition must have been hit (we can ignore an integer overflow in this case).
1274+
\par The results and time consumed to complete the program are radically
1275+
different for situations when the program used a plain unprotected addition,
1276+
atomic arithmetic functions, and locking using mutexes. The difference in
1277+
completion times between arithmetic operation functions and mutexes is
1278+
especially notable on CPUs with hardware parallelism support.
1279+
\item Similarly there is an API for subtraction, bit operations AND and OR,
1280+
value assignment, etc.
1281+
\item The atomic primitives and types in C11 is an optional feature, so should
12841282
be wrapped under \texttt{\_\_STDC\_NO\_ATOMICS\_\_} negative define.
1285-
\item Good description of the C11 atomic API can be found on
1283+
\item A good description of the C11 atomic API can be found on
12861284
\url{http://en.cppreference.com/w/c/atomic}
12871285
\end{itemize}
12881286

0 commit comments

Comments
 (0)