Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

Is "lock" opcode prefix of no use because of "cache coherency mechanism"?

Intel manual says:

8.1.4 Effects of a LOCK Operation on Internal Processor Caches
...
The cache coherency mechanism automatically prevents two or more processors that have cached the same area of memory from simultaneously modifying data in that area.

Does that mean that lock opcode prefix is useless for application programming?

The CPU cache is always turned on, and all application-accessible memory is cacheable, so cache coherency mechanism is always on-guard to protect integrity of your data stored in shared memory.

"Cache coherency mechanism" is an inherent part of multi-core CPU's cache, it works always independently of whether you are using lock prefix or not.

More detailed question:
I have Core2Duo CPU, and my application program runs in 2 parallel threads working with common global variables.
Is it safe for me to skip lock prefix in instructions like lock add [ESI],EAX and lock cmpxchg8b [ESI]?

Upvotes: 1

Views: 211

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39571

No, without the LOCK prefix the cache lock won't be held across the entire read/modify/write operation. This means the operation won't appear as atomic to other processors. The LOCK prefix also enforces strict memory ordering rather than the somewhat more looser strong memory ordering normally used.

Upvotes: 6

Related Questions