On Fri, 2005-Oct-14 10:10:04 +0100, Brian Candler wrote: >I'd be grateful if you could clarify that point for me. Are you saying that >if I write > > long long foo; > ... > foo++; > >then the C compiler generates code for 'foo++' which is not thread-safe? >(And therefore I would have to protect it with a mutex or critical section) foo++ is not normally thread-safe even if foo is an int or pointer. Typical iA32 code looks like: addl $1,foo This is only thread-safe in a UP environment. Typical RISC code looks like: load foo,%reg add $1,%reg store %reg,foo This is not thread-safe. Note that the compiler may keep foo in a register for an extended period unless you convince the compiler not to. Especially on RISC processors, the compiler will normally spread the load/add/store to try and avoid pipeline stalls. If you share foo between two threads, you need to use atomic operations (see <machine/atomic.h>), mutexes or critical sections on all updates. Note that you can do atomic 64-bit operations on iA32 (except 80386 and 80486) using a locked cmpxchg8b in a loop. -- Peter JeremyReceived on Fri Oct 14 2005 - 08:30:04 UTC
This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:45 UTC