# Atomicity Violation in Multithreaded Applications and Its Detection in Static Code Analysis Process
## 8 Leading Example  
The atomicity violation can be found in the sample AV1 application (source code: [[http://bit.ly/2P849](http://bit.ly/2P849) ma]([http://bit.ly/2P849](http://bit.ly/2P849) ma)). To do that there is only the need to use the _rdao_detector_ ([[https://github.com/PKPhdDG/rdao_detector](https://github.com/PKPhdDG/rdao_detector)]([https://github.com/PKPhdDG/rdao_detector](https://github.com/PKPhdDG/rdao_detector))), application which uses MASCM to detect, among other things, the phenomenon of atomicity violation. The detection discussed in the previous section is implemented as an algorithm in the author's tool and is used to optimize code. The code used is equivalent to a piece of TCP/IP server application and was running on an embedded device. The server was run as a data collector in a commercial environment. Two running threads were responsible for handling requests from two different sensor groups. In the presented example there are many simplifications to show an idea that is significant for multithreaded applications. After converting the application to a model described in Section 5, it looks as follows. \[ \begin{array}{l}T_{AV1}=(t_{0},t_{1},t_{2});\\ U_{AV1}=(\{t_{0}\},\{t_{1},t_{2}\},\{t_{0}\});\\ R_{AV1}=\{(r1)\};\end{array} \]  
Figure 7: Subgraph showing the first scenario of atomicity violation. \(O_{AV1}=\{o_{0,1},o_{0,2},o_{0,3},o_{0,4},o_{0,5},o_{1,1},o_{1,2},o_{1,3},o_{1,4},o_ {1,5},o_{1,6},o_{1,7},o_{1,8},o_{1,9},o_{2,1},o_{2,2},o_{2,3},o_{2,4},o_{2,5},\)  
\(o_{2,6},o_{2,7},o_{2,8},o_{2,9}\};\)  
\(Q_{AV1}=\{(m,PMD)\};\)  
\(F_{AV1}=\{(o_{0,1},o_{0,2}),(o_{0,2},o_{0,3}),(o_{0,3},r_{1}),(o_{0,3},o_{0,4}), (o_{0,4},r_{1}),(o_{0,4},o_{0,5}),(o_{1,1},o_{1,2}),(q_{1},o_{1,2}),\)  
\((o_{1,2},o_{1,3}),(o_{1,3},r_{1}),(o_{1,3},o_{1,4}),(o_{1,4},q_{1}),(o_{1,4},o_ {1,5}),(o_{1,5},o_{1,6}),(q_{1},o_{1,6}),(o_{1,6},o_{1,7}),\)  
\((r_{1},o_{1,7}),(o_{1,7},o_{1,8}),(o_{1,8},q_{1}),(o_{1,8},o_{1,1}),(o_{1,1},o _{1,9}),(o_{1,8},o_{1,9}),(o_{2,1},o_{2,2}),(q_{1},o_{2,2}),\)  
\((o_{2,2},o_{2,3}),(o_{2,3},r_{1}),(o_{2,3},o_{2,4}),(o_{2,4},q_{1}),(o_{2,4},o _{2,5}),(o_{2,5},o_{2,6}),(q_{1},o_{2,6}),(o_{2,6},o_{2,7}),\)  
\((r_{1},o_{2,7}),(o_{2,7},o_{2,8}),(o_{2,8},q_{1}),(o_{2,8},o_{2,1}),(o_{2,1},o _{2,9}),(o_{2,8},o_{2,9})\};\)  
\(B_{AV1}^{BWD}=\{(o_{1,3},o_{1,7}),(o_{2,3},o_{2,7})\}.\)  
In AV1, there is a backward relationship between the _printf_ function and the incrementation operation. As a result of its presence, the user is likely to receive incorrect data on the standard output. Locating the atomicity violation phenomenon consists of several steps, after making sure that the application is free of resource conflicts causing race condition or deadlock. In the first step, it is necessary to determine whether there are intervals in which more than one thread works, which is true for AV1 applications. Then, as a second step, one should determine which of the pairs belonging to the sets of sequences of \(B_{AV1}\) can be violated by changing the shared resource. For AV1 applications these are pairs:  
1. \((o_{1,3},o_{1,7})\) can be violated by \(o_{2,3}\);
2. \((o_{2,3},o_{2,7})\) can be violated by \(o_{1,3}\). In step three, it should be determined whether the pairs coming from the sets of \(B_{AV1}\) sequences meet one of the scenarios in Table 1. If the pair meets any scenario, it should be determined whether there is a path in the operation graph that meets one of the lemmas described in Section 7. In the case of AV1 applications, there are two such paths that meet Lemma 1 and both of them start on a mutex \(q_{1}\). As a result of the fulfilment of Lemma 1, AV1 can therefore be defined as an incorrect application--i.e., one in which there are resource conflicts causing the phenomenon of atomicity violation. On the basis of the leading example, it can therefore be concluded that the use of a model to monitor the source code of a multithreaded application allows for the detection of the atomicity violation phenomenon.