On Fri, Oct 9, 2009 at 12:53 AM, Matt Brooks <mattbrooks / gatech.edu> wrote:
> All Day I have tried to accomplish the following using threads, and just
> could not get it to work.        
> the power to do stuff....
>
> I want two threads to execute and fight each other to send commands to a
> unit.        
> I want them to send the commands at the same time.
>
> I guess I am realizing that isn't possible, only one will have control
> at a time, thats fine, I want them to pass control to each other
> continously, seemingly randomly to me from the outside.
>
> Also in both threads i have a forever loop.
>
> so for example
>
> a = Thread.new do
> loop do
> method that sends commands, about 60 of them
> end
> end
>
>
> b = Thread.new do
> loop do
> method that sends different commands, about 20 of them
> end
> end
>
>
>
> no matter what, thread a only runs a few commands before thead b takes
> over forever, and thread a never executes again.

This works for me:

$ cat threads.rb
a = Thread.new do
10.times do
puts "command from A"
sleep rand
end
end

b = Thread.new do
10.times do
puts "command from B"
sleep rand
end
end

a.join
b.join


$ ruby threads.rb
command from A
command from B
command from B
command from A
command from A
command from B
command from B
command from A
command from B
command from A
command from B
command from A
command from A
command from B
command from A
command from B
command from A
command from B
command from B
command from A


Seems random to me...

Hope this helps,

Jesus.