|
1259 | 1259 | \item \label{ATOMIC_ADD} Example \example{race/atomic-add.c} demonstrates |
1260 | 1260 | the race condition problem when performing addition and its possible solutions. |
1261 | 1261 | 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: |
1265 | 1265 |
|
1266 | 1266 | \begin{verbatim} |
1267 | 1267 | for (i = 1; i < arg; ++i) |
1268 | 1268 | x = x + i; |
1269 | 1269 | \end{verbatim} |
1270 | 1270 |
|
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 |
1284 | 1282 | 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 |
1286 | 1284 | \url{http://en.cppreference.com/w/c/atomic} |
1287 | 1285 | \end{itemize} |
1288 | 1286 |
|
|
0 commit comments