Thread problem

J

Jaeyong Lee

Hi all,

I learning ruby thread and faced to thread problem.

Here's my test code that sum 1 to 1000.

--
threads = []

sum = 0

1.upto(1000) { |external|
threads << Thread.new(external) { |local|
sleep(rand(0.01))
sum = sum + local
}
}

threads.each() { |thread| thread.join() }

puts sum
 
Y

Y. NOBUOKA

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*. You need to use a Mutex object to avoid the simultaneous use of
that variable.
cf. Mutex class: http://ruby-doc.org/core/classes/Mutex.html

---
require 'thread'
threads =3D []

sum =3D 0
mutex =3D Mutex.new

1.upto(1000) { |external|
threads << Thread.new(external) { |local|
sleep(rand(0.01))
mutex.synchronize {
sum =3D sum + local
}
}
}

threads.each() { |thread| thread.join() }

puts sum
---

2010/11/17 Jaeyong Lee said:
Hi all,

I learning ruby thread and faced to thread problem.
Here's my test code that sum 1 to 1000.

--
threads =3D []

sum =3D 0

1.upto(1000) { |external|
=A0threads << Thread.new(external) { |local|
=A0 =A0sleep(rand(0.01))
=A0 =A0sum =3D sum + local
=A0}
}

threads.each() { |thread| thread.join() }

puts sum

--=20
NOBUOKA Yuya
 
J

Justin Collins

Hi all,

I learning ruby thread and faced to thread problem.

Here's my test code that sum 1 to 1000.

--
threads = []

sum = 0

1.upto(1000) { |external|
threads<< Thread.new(external) { |local|
sleep(rand(0.01))
sum = sum + local
}
}

threads.each() { |thread| thread.join() }

puts sum
--

I expected '500500' but output is smaller than. (ex: 499864)

Please let me know what problem is.

http://en.wikipedia.org/wiki/Race_condition#Computing

You are updating a shared variable "simultaneously" in separate threads,
which is a very bad idea.

-Justin
 
R

Robert Klemme

Hi all,

I learning ruby thread and faced to thread problem.

Here's my test code that sum 1 to 1000.

--
threads =3D []

sum =3D 0

1.upto(1000) { |external|
=A0 threads<< =A0Thread.new(external) { |local|
=A0 =A0 sleep(rand(0.01))
=A0 =A0 sum =3D sum + local
=A0 }
}

threads.each() { |thread| thread.join() }

puts sum
--

I expected '500500' but output is smaller than. (ex: 499864)

Please let me know what problem is.

http://en.wikipedia.org/wiki/Race_condition#Computing

You are updating a shared variable "simultaneously" in separate threads,
which is a very bad idea.

Absolutely correct. In this particular case using a shared resource
is not necessary since the state of that shared resource is only
accessed when all threads have terminated. One could do this as well:

threads =3D []

1.upto(1000) { |external|
threads << Thread.new(external) { |local|
sleep(rand(0.01))
local
}
}

sum =3D 0

threads.each() { |thread| sum +=3D thread.value()}

puts sum

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
B

Brian Candler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top