Duncan Booth schrieb:
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:

On Thu, 01 May 2008 15:33:09 -0700, Gary Herron wrote:

Of course it's not thread safe. For the same reason and more basic, even the expression i++ is not thread safe in C++.

Any such calculation, on modern processors, requires three operations: retrieve value of i into a register,
  increment the register
  write the value into i.
There are no modern processors with an opcode for incrementing a memory
location!?  At least my C64 can do that.  ;-)

The operation i++ in C/C++ implies two things: it increments the memory location and returns the new result. That means there are at least two ways to generate code for this:

   increment memory
   load new value

or

   load value
   incremenent
   store

Neither of these is going to be thread-safe (even on a C64) unless you protect the operations somehow. The latter is probably preferred as it only implies two operations to memory whereas the former implies three.

This is not entirely right. increment is usually atomic (at least for single-core processors), so you don't suffer from a load/store being interrupted.

Of course it can happen that the previous or later loading of the value for computational purposes is interrupted by another thread. But you have the guarantee that all incs (and decs) are counted, in contrast to what the OP observed with python.

Diez
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to