You are here:

C++/is assigmnent for int atomic

Advertisement


Lee wrote at 2009-04-03 18:50:04
Yes but a read is one operation and a write is another, so let suppose that 2 cores read at the same value, and then they increment it and store. That's at least 3 instructions.



CPU 1       CPU 2

Read 0001

         Read 0001

Inc  0002  

         Inc 0002

Write 0002

         Write 0002



An Atomic Increment would effectively block the read until the increment is complete.



CPU 1         CPU 2



Read 0001     Waiting For Atomic Lock

Inc  0002

Write 0002

         Read 0002

         Inc  0003

         Write 0003


MichaelBinary wrote at 2010-12-07 17:01:53
That is not true.

If the int is lets say word(32 bit) aligned then it is an atomic update. However, if the int is 16 bit aligned the processor will write the data to memory in 2 16 bit chunks, allowing the possibility for another process to read the first 16 bits changed, but the old value for the second 16 bits.


C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2012 About.com, a part of The New York Times Company. All rights reserved.