Thread Variable race condition

K

Karan Rajput

Hi,


I am not enough efficient in Threads.
Here is the problem.


i = 1
while(i < 100)

t = Thread.new do
echo(i)
end
t.join if (i % 5 == 0)
i = i + 1
end

def echo(num)
puts num
end

I get output sometimes as,

1
2
3
5
5 ==> 5 is getting duplicated whereas 4 is dropped. I feel its race
condition for variable.
6.... and so on.

This error occurred for any random number.

Sometime i get the correct output.

Can anybody suggest what will be solution to avoid any such problem.
Will replacement of Thread with Process will solve problem?
I am hoping solution in Threads only if its possible.


Thanks in Advance.
 
R

Robert Klemme

Hi,


I am not enough efficient in Threads.
Here is the problem.


i =3D 1
while(i < 100)

=A0 t =3D Thread.new do
=A0 =A0 =A0echo(i)
=A0 end
=A0 t.join if (i % 5 =3D=3D 0)
=A0 i =3D i + 1
end

def echo(num)
puts num
end

I get output sometimes as,

1
2
3
5
5 =3D=3D> 5 is getting duplicated whereas 4 is dropped. I feel its race
condition for variable.

No, this is not an indicator of a race condition. Even with proper
synchronization it may happen that two threads that you started get
scheduled before the next increment occurs and they thus print the
same value.
6.... and so on.

This error occurred for any random number.

It's not an error - although your code is of course not thread safe
because you do not synchronize.
Sometime i get the correct output.

What you consider a "correct output" (I assume a sequence where each
printed value is one higher than the previous value) is no more
correct than what you see. You may even see output where the order is
reversed (e.g. 1 2 4 3 5) because any thread might be interrupted at
any point between fetching the value and printing it. Granted, it's
not too likely but perfectly valid and must be expected.
Can anybody suggest what will be solution to avoid any such problem.
Will replacement of Thread with Process will solve problem?

Certainly not. With processes you don't have shared memory any more.
You would put in more effort and make processes communicate with each
other (e.g. via DRb) - typically one process will hold the counter and
others would do the printing. But even then you need proper
synchronization.
I am hoping solution in Threads only if its possible.

The question is: for which problem do you need a solution? If you
just want to print a sequence of values you don't need threads. You
can just do

100.times {|i| puts i}

Kind regards

robert

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

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top