Re: multi thread app and often variable accessing



On Fri, 2007-06-08 at 19:41 -0600, Jim George wrote:
Begin Curiosity:
Also, the hardware must have something like the Pentium CMPXCHG8B [...]
Is it not
sufficient to be able to write an int in one single bus access? ie
have a 32-bit wide data bus
Or is that exactly the point? :D

No, in most cases, if you want to atomically increment or decrement a
value (for example, a semaphore or ref count) without locking, you
have to read the value, increment it, then write it only if it's not
changed. The write and check to see if it's not changed must be
atomic, this is where CMPXCHG comes in.

MOV instructions with a bus-lock prefix, which is slow.
OK, I guess it'd be faster (and leaner) than using a mutex though
But that can't be done in pure C, right?

Maybe, but I don't see how any end-user program (such as one with a
GTK interface) would benefit from the small speed increase. Put such
stuff in another program which merely reports its progress to the gtk
GUI program through a socket or pipe.

Another effect of using the CMPXCHG is that it forces the memory manager
to read the value from memory and not cache, and write through the cache
to physical memory on write.  This is important in the case where you've
got more than one core running threads on your code: Each core has it's
own cache.  If the instruction didn't force a sync with physical memory,
your code would break.


  // Wally

-- 
Wallace Owen L-3Com com
Office: 619.278.2084
Cell:   619.990.2286



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]