I see in some answers / questions the term "atomic".
In programming, what does it really mean?
Ex:
I see in some answers / questions the term "atomic".
In programming, what does it really mean?
Ex:
Atomic comes from the atom, ie something that is indivisible. In computing we use the term when something is guaranteed to be done in its entirety, it can not do only a part, even if it has to discard what it was doing. An atomic operation can not be divided into parts without causing any problems.
Refreshing an integer value is an atomic operation on most architectures. you do not change a part of it. Changing a decimal
or long
may already be atomic because it can have more than one part and if it does not have any mechanism that guarantees the atomicity one part can be changed, something happens next with this value and then another part to be updated. This is also called linearization .
This has to do with the state. The concern is not whether the algorithm itself was executed completely, but whether the state was completely changed. What does not change state has no atomicity problems. So what a method returns has nothing to do with this concept, will have if the method changes some state within it. If this change is made completely or nothing is done it will be atomic. If he lets change a part for some reason it is not atomic.
The fact of making the change of one part leaves this part visible by some concurrent operation and then doing the other part also breaks the atomicity (although it has more to do with isolation), even if the operation is complete. If more than one part is divisible, then it is not atomic. So there are mechanisms to control that several parts run together completely before letting something access that. Locking is the most obvious mechanism.When there is no form of competition atomicity is not important. The problem is that there is competition in several places where we do not see. And the fact that there is no competition does not mean that something can not be atomic. Either something is atomic or it is not. If you needed to be is another matter.
It has to do with race condition . It is rare to observe this problem on certain occasions and this causes people to neglect the problem. He is worse just because he is rare. It is difficult to reproduce and know what is happening. So it has to do right even though it does not seem to be necessary.
The word atom comes from the Greek atoms . It is made up of the parts:
So an atom is something that is impossible to divide (until Albert Einstein comes along and sculpt with that part of physics).
In programming, we say that one thing is atomic if it can not be broken into more parts. Usually this is used in databases, for a sequence of operations that should be treated as an operation only for information consistency reasons.
For example, suppose you want to transfer money through Internet Banking . The sequence of steps in the database would be as follows:
1-) Your balance is subtracted from the amount to be transferred;
2-) My balance is increased by the same amount;
3-) an outbound log is posted to your account log;
4-) an entry record is posted to the log of my account.
What happens if the connection falls between step 1 and step 2? Your money disappears and goes limbo, without ever reaching my account, and without the exit log can not recover that amount.
In making this atomic sequence, the bank ensures that either it happens, or it does not happen, because it is indivisible and all steps must occur as one. In practice, this means that the operations are in memory and are only persisted all at once - if an operation fails, all operations are reversed.
Atomicity: A transaction must be an atomic unit of work; or all of your data modifications are performed, or none of them is performed. source: Microsoft Technet
Note: You need to know what a thread is to understand the explanation below.
All instructions you make on a computer are divided into parts, this is how the computer architecture is built, for example:
if(x == 3){
// faz alguma coisa importante aqui e depois zera o x
x=0;
}else{
x++
}
In order for the computer to be able to scan x == 3 it splits it into 8 parts.
Knowing this, imagine a program that has several threads, this causes what we call competition situation, for example: let's say there are only two threads, and the first one comes in if(x==3)
and then it will zero the value of x but this action is not atomic (ie it is not executed at one time.) and the second thread also arrives at if before the first succeed in zeroing x or it will also enter the if