Community
cancel
Showing results for 
Search instead for 
Did you mean: 
OTorg
New Contributor II
148 Views

Validity of InterlockedCompareExchange64 + Read32 operations mix.

Objective:

I have a chunk of memory shared between several x86/x64 processes/threads.

Inside it I have QWORD (64 bit) variable aligned on 8-byte boundary.

That variable is accessed by one writer and several readers simultaneously, mix of x86 and x64 readers/writers is possible.

Let's say that variable is declared/named as QWORD qwSharedVar, and compiler is MSVC with /volatile:ms behaviour.


Suppositions:

1.
If writer process is x64, it writes a new value in a such way:

*reinterpret_cast< volatile QWORD* >( &qwSharedVar ) = qwNewValue;


2.
If writer process is x86, it writes a new value in a such way:

_InterlockedCompareExchange64( reinterpret_cast< volatile __int64* >( &qwSharedVar ), qwNewValue, qwSharedVar );


3.
If reader process is x64, it reads a shared value in a such way:

QWORD qwValueNow = *reinterpret_cast< volatile QWORD* >( &qwSharedVar );


4.
If reader process is x86, it reads a shared value in a such way:

QWORD qwValueNow = _InterlockedCompareExchange64( reinterpret_cast< volatile __int64* >( &qwSharedVar ), 0, 0 );


5.
If reader process is x86 or x64, and it wants to read only lower DWORD of shared value, then:

DWORD dwValueNow = *reinterpret_cast< volatile DWORD* >( &qwSharedVar );


Are those suppositions legal?

I'm especially worried about point 5, where mix of accesses through "lock cmpxchg8b" and "mov dword ptr" is possible.

0 Kudos
3 Replies
Alberto_Sykes
Employee
137 Views

OTorg, Thank you for posting in the Intel® Communities Support.


In order for us to be able to provide the most accurate assistance on this case, we just wanted to confirm a few details:

Are you a Software Developer?

Are you developing an application?

Are you working on a project?

What is the name of the project/application?

What is that project/application about, could you please provide a description of it?


Any questions, please let me know.


Regards,

Albert R.


Intel Customer Support Technician

A Contingent Worker at Intel


OTorg
New Contributor II
123 Views

Yes, I'm a software developer. I can't provide the name of the project and other similar information (as it seems to me, not relevant to the essence of the question), because no project exists. I want to understand the architectural nuances in order to use them correctly in the future.

And while you are carefully thinking about the name of the project, I have already received adequate answers to my question from stackoverflow community:)

Alberto_Sykes
Employee
118 Views

OTorg, Thank you very much for clarifying those details.


Perfect, excellent it is great to hear that you already received the information requested from the stackoverflow community.


If by any chance you need further assistance or have more questions, please visit, sign-in and submit your question in the Intel® Developer Zone:

https://software.intel.com/content/www/us/en/develop/home.html


Regards,

Albert R.


Intel Customer Support Technician

A Contingent Worker at Intel


Reply