Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.
1697 Discussions

lock prefix for logical cpus on the same physical cpu...

Chris_M__Thomasson
New Contributor I
499 Views
I think I know the answer already but I wanted to be sure. I believe that if you have two threads T1-T2that are bound toalogical processor LP1, they can atomically modifyper-processor data that is owned by LP1by using a cmpxchg without a lock-prefix. Now, here ismy question:
Given the following senerio:
Logicalprocessor LP1 isownedby core PC1 on physical processor P1
Logicalprocessor LP2 isownedby core PC1 on physical processor P1
Per-processor data D is owned by PC1
Thread T1 is bound to LP1
Thread T2 is bound to LP2
Could T1 and T2 use cmpxchg without a lock prefixto modify D?
0 Kudos
3 Replies
Chris_M__Thomasson
New Contributor I
499 Views

Could T1 and T2 use cmpxchg without a lock prefixto modify D?



I think the answer is no, because cmpxchg does a redundant write that can probably be observed with the lock prefix...
0 Kudos
Chris_M__Thomasson
New Contributor I
499 Views


lockfree wrote:

Could T1 and T2 use cmpxchg without a lock prefixto modify D?



I think the answer is no, because cmpxchg does a redundant write that can probably be observed with the lock prefix...


DOH! I mean't: cmpxchg does a redundant write that can probably be observed "without" the lock prefix... Sorry.
0 Kudos
ClayB
New Contributor I
499 Views

I posed this question to Phil Kerly, one of the Intel experts on Hyper-Threading. His response was that cmpxchg is NOT HT-safe without the 'lock' prefix. He sent a quick example that demonstrates this. I've attached the code to this post.


Phil's instructions for using this demo code:

Find the line #define LOCK_EXAMPLE in the attached Windows file which I hacked together. You can uncomment this line to remove the lock prefix.

With the lock prefix, the program will run indefinitely until you enter ctrl-C to stop it. Without the lock prefix, the program will terminate once is detects an unexpected condition.

Two threads are created and affinitized to each of the logical HT processors. Each thread updates 16-bits of a 32-bit quantity using cmpxchg. One thread only updates the upper half. The other thread only updates the lower half. If at any time either thread detects that its portion does not match its expected value, the program will exit with a message stating that there has been a violation.


I hope this helps.

--clay

0 Kudos
Reply