Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I convert non atomic operation to atomic

I am trying to understand atomic and non atomic operations.With respect to Operating System and also with respect to C. As per the wikipedia page here

Consider a simple counter which different processes can increment.
Non-atomic

The naive, non-atomic implementation:
reads the value in the memory location;
adds one to the value;
writes the new value back into the memory location.

Now, imagine two processes are running incrementing a single, shared memory location:
the first process reads the value in memory location;
the first process adds one to the value;
but before it can write the new value back to the memory location it is suspended, and the second process is allowed to run:
the second process reads the value in memory location, the same value that the first process read;
the second process adds one to the value;
the second process writes the new value into the memory location.

How can the above operation be made an atmoic operation. My understanding of atomic operation is that any thing which executes without interruption is atomic. So for example

int b=1000;
  b+=1000;

Should be an atomic operation as per my understanding because both the instructions executed without an interruption,how ever I learned from some one that in C there is nothing known as atomic operation so above both statements are non atomic. So what I want to understand is what is atomicity is different when it comes to programming languages than the Operating Systems?

like image 299
Registered User Avatar asked Jul 10 '11 07:07

Registered User


People also ask

What makes an operation atomic?

Atomicity. In computer programming, an operation done by a computer is considered atomic if it is guaranteed to be isolated from other operations that may be happening at the same time. Put another way, atomic operations are indivisible. Atomic operations are critically important when dealing with shared resources.

How do you ensure atomic operations?

There are two ways to accomplish an atomic operation: Use a single machine instruction. Tell the operating system in advance not to interrupt the operation.

What is non atomic operation?

Non-Atomic CPU InstructionsA memory operation can be non-atomic even when performed by a single CPU instruction. For example, the ARMv7 instruction set includes the strd instruction, which stores the contents of two 32-bit source registers to a single 64-bit value in memory.

Is ++ an atomic operation?

On objects without an atomic type, standard never defines ++ as an atomic operation.


1 Answers

C99 doesn't have any way to make variables atomic with respect to other threads. C99 has no concept of multiple threads of execution. Thus, you need to use compiler-specific extensions, and/or CPU-level instructions to achieve atomicity.

The next C standard, currently known as C1x, will include atomic operations.

Even then, mere atomicity just guarantees that an operation is atomic, it doesn't guarantee when that operation becomes visible to other CPUs. To achieve visibility guarantees, in C99 you would need to study your CPU's memory model, and possibly use a special kind of CPU instructions known as fences or memory barriers. You also need to tell the compiler about it, using some compiler-specific compiler barrier. C1x defines several memory orderings, and when you use an atomic operation you can decide which memory ordering to use.

Some examples:

/* NOT atomic */
b += 1000;

/* GCC-extension, only in newish GCCs 
 *   requirements on b's loads are CPU-specific
 */
__sync_add_and_fetch(&b, 1000);

/* GCC-extension + x86-assembly, 
 *   b should be aligned to its size (natural alignment), 
 *   or loads will not be atomic
 */
__asm__ __volatile__("lock add $1000, %0" : "+r"(b));


/* C1x */
#include <stdatomic.h>
atomic_int b = ATOMIC_INIT(1000);
int r = atomic_fetch_add(&b, 1000) + 1000;

All of this is as complex as it seems, so you should normally stick to mutexes, which makes things easier.

like image 128
ninjalj Avatar answered Oct 13 '22 01:10

ninjalj