What does the term "atomic" mean?

Asked

Viewed 1,634 times

16

  • 1

    Sorry, @Renan. It was a mix-up between my issues and yours.

  • 1

    @LINQ in the problemo.

4 answers

19


Atomic comes from the atom, that is, something that is indivisible. In computing we use the term when something is guaranteed to be done completely, you can’t just do a part, even if you have to discard what you were doing to ensure that. An atomic operation cannot be divided into parts without causing some problem.

Updating an integer value is an atomic operation on most architectures. You do not change a part of it. Changing a decimal or long It may no longer be atomic because it can have more than one part, and if it has no mechanism that guarantees atomicity, one part could be changed, and something happened afterwards with that value and then another part was updated. This is also called linearization.

In a database the term is used a lot, mainly in transactions. This means that either the transaction does completely or it does nothing, it cannot do only a part. If you make a purchase the operation has to issue the invoice, change the balance of the stock, download the order, generate the ticket, etc., all together, if something fails none of this can be done, can not make some of these and leave the others for there.

This has to do with the state. The concern is not whether the algorithm itself was executed entirely, but whether the state was changed altogether. What does not change state has no atomicity problems. So what a method returns has nothing to do with this concept, it 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 it lets change only a part for some reason it is not atomic.

The fact of making the change of one party make this altered part visible by some competing operation and then make 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 various parts run together completely before letting something access it. Locking is the most obvious mechanism.

When there is no form of competition atomicity is not important. The problem is that there is competition at various points where we do not see. And the fact that there is no competition does not mean that something cannot be atomic. Either something is atomic or it is not. Whether it needed to be is another question.

It also has to do with running condition. It is rare to observe this problem at certain times and it makes people neglect the problem. It is worse precisely because it is rare. It’s hard to reproduce and know what’s going on. That’s why you have to do it right even though it doesn’t seem to be necessary.

  • So a method that returns true or nothing is atomic, and a method that can return true, false or an exception wouldn’t be, is this ?

  • No, nothing to do with it, I edited it to make it better.

10

The word atom comes from the Greek atoms. It is formed by the parts:

  • to: radical indicating denial;
  • tomos: which means divisions.

Therefore, an atom is something that is impossible to divide (until it appears albert Einstein Marie Curie and the physics part).

In programming, we say that a thing is atomic if it cannot be broken into more parts. Generally this is used in databases, for a sequence of transactions that should be treated as a transaction only for information consistency reasons.

For example, suppose you want to transfer me 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 output log is launched in your account log;
4-) an entry log is launched in my account log.

What happens if the connection falls between step 1 and step 2? Your money disappears and goes into limbo, without ever reaching my account, and without the log of output there is no way to recover that amount.

By 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 operations stay in memory and are only persisted all at once - if an operation fails, all operations are reversed.

4

Atomicity: A transaction must be an atomic unit of work; either all your data modifications are executed or none of them are executed. source: Microsoft Technet

4

Note: It is necessary that you know what a thread is to understand the explanation below.

All the instructions you do on a computer are divided into parts, this happens by the way the computer architecture is built, for example:

if(x == 3){
    // faz alguma coisa importante aqui e depois zera o x
    x=0;
}else{
    x++
}

so that the computer is able to do the x == 3 he splits it into 8 parts.

Knowing this, imagine a program that has several threads, this causes what we call a competitive situation, for example: let’s say there are only two threads, and the first one comes in if(x==3) and soon after it will reset the value of x but this action is not atomic(or is not executed at once. ) and the second thread also arrives at if before the first thread can reset the x that is she will also enter the if

So when we work with threads we want some actions to be "atomic", which in theory means that it would be executed at once, but this is impossible because it is part of the architecture of the computer, but the operating system can "circumvent" the architecture and makes that point that was signaled as atomic is not accessed by another thread until it is released. This block and release is done through semaphores or mutex, in java you have classes that abstract this implementation for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.