From: Brian Candler Date: 2010-11-17T20:11:52+09:00 Subject: Re: Thread problem Y. NOBUOKA wrote in post #962070: > I think that the reason of this problem is the simultaneous use of a > common resource. In your example, the common resource is the variable > *sum*. Or more explicitly: sum = sum + local is not an atomic operation. Consider it as a sequence of steps: 1. retrieve value of 'sum' 2. retrieve value of 'local' 3. add them 4. store the result back in 'sum' Now think what happens when two threads are doing this together. Let's say the sum is currently 15, and the two threads want to add 6 and 7. Thread A might get as far as completing steps 1-3, reading the value of sum (15) and local (6) and computing the sum, then get switched out. Thread B runs. It reads sum (15) and local (7), and computes the sum. It stores 22 into 'sum'. Thread A now resumes. It stores 21 into 'sum', overwriting the value stored by Thread B. Regards, Brian. -- Posted via http://www.ruby-forum.com/.