From: Robert Klemme Date: 2011-01-13T23:24:09+09:00 Subject: Re: Thread Variable race condition On Thu, Jan 13, 2011 at 11:59 AM, Karan Rajput wrote: > 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. 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/