This is a simplified version of some code that I currently support:
int SomeFunc() { const long lIndex = m_lCurrentIndex; int nSum = 0; nSum += m_someArray[lIndex]; nSum += m_someArray[lIndex]; return nSum; }
lCurrentIndex is periodically updated by another thread. The question is; will create a local copy of m_CurrentIndex, make sure that both calls to m_someArray use the same index?
Note that this is a simplified example; I’m thinking about the concept of creating a local copy, and not about which code is shown here. I know that the compiler will put this value in a register, but it is still a local copy, and not reading from lCurrentIndex twice.
Thanks!
Edit: the initial assignment is safe, both guarantees are guaranteed to be 32 bits in our setup. Edit2: And they are correctly aligned on the 32-bit border (forgot about it)
, , , . (, , 8- ). , - , / , .
m_CurrentIndex , to m_someArray ?
SomeFunc, , . (lIndex) .
SomeFunc
lIndex
, : m_someArray[lIndex] ( lIndex) ; m_ someArray ; lIndex.
m_someArray[lIndex]
someArray
, , . , "". . , .
( , / , ). , , m_lCurrentIndex volatile. , .
m_lCurrentIndex
volatile
, , - m_lCurrentIndex lIndex ? , , , , .:)
: , .
, , .
, . , " " m_lCurrentIndex lIndex .
x86 , m_lCurrentIndex DWORD , long DWORD ( x86), , . , x64 , long DWORD m_lCurrentIndex, DWORD long 64b, m_lCurrentIndex 64b, , . .
, CAS ( , , , , , ), , , .
: , , , , . , , . .
, . , m_lCurrentIndex . , , m_lCurrentIndex, . , , , lIndex. , m_lCurrentIndex lIndex .
Source: https://habr.com/ru/post/1713736/More articles:Эндиантность порядка байтов IBM PC? - endiannessHow to achieve data binding using a user control in WPF? - c #UIScrollView goes under the navigation and status bar - iphone-sdk-3.0stop scrolling viewviewcontroller table - objective-cRounded corners in jquery images - jqueryDrawing a contrast line in an image - stringWhat options are available for VisualSVNServerHooks.exe? - svnRefresh all rows of one column - sqlОсновные предложения по проекту - computer-scienceCan my Java software be converted to .exe software? - javaAll Articles