in reply to Re^3: using parallel processing to concatenate a string, where order of concatenation doesn't matter
in thread using parallel processing to concatenate a string, where order of concatenation doesn't matter

Race conditions can occur even if only one thread is running a time. What matters is when one thread can interrupt another thread.

Consider two threads doing i+=1 in C.

i+=1 CPU ----------+---------- thread 1 | thread 2 ==========+========== ... | T load i | i add 1 | m ----------+---------- e | ... | | load i | | add 1 v | save i | ... ----------+---------- save i | ... |

Even though both threads have run, and even though only one thread executed at a time, i was only incremented by one.

So my question remains. Is .= atomic (i.e. uninterruptable, safe) or does it introduce a race condition (like C's i+=1, requiring the use of locks)?

Update: Answered in Threads: why locking is required when using shared variables.

  • Comment on Re^4: using parallel processing to concatenate a string, where order of concatenation doesn't matter
  • Select or Download Code