Green Threads - I want them to thrash

All Day I have tried to accomplish the following using threads, and just
could not get it to work. I want them to thrash back and forth having
the power to do stuff....

I want two threads to execute and fight each other to send commands to a
unit. With each thread they just call methods with certain arguments.
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.

To combat the problem I tried doing this after the creation of the
threads, in main I guess you call it...

  loop do
  a.priority = -1
  b.priority = -2
  sleep(50)
  b.priority = -1
  a.priority = -2
  end

I also tried positioning Thread.pass randomly throughout the two
threads, hoping that would tell the thread, hey let the other guy have a
chance, then that thread would get to his Thread.pass meaning let the
first guy have a chance to send some stuff!!

All these ways have some outcome, just the second thread executes
forever....

Any ideas, thank you very much for the help!!

···

--
Posted via http://www.ruby-forum.com/.

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.

···

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. I want them to thrash back and forth having
the power to do stuff....

I want two threads to execute and fight each other to send commands to a
unit. With each thread they just call methods with certain arguments.
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.

You did not mention the version you are using, so I assume it was
1.8ish. It does work on 1.9, but it takes some time:

17:00:10 desktop$ cat th.rb

require 'thread'

m = Mutex.new

(1..2).map do |i|
  Thread.new i do |ii|
    20000.times do
      m.synchronize do
        puts ii
      end
    end
  end
end.each {|th| th.join}

17:00:16 desktop$ allruby th.rb | uniq
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

···

2009/10/9 Matt Brooks <mattbrooks@gatech.edu>:

All Day I have tried to accomplish the following using threads, and just
could not get it to work. I want them to thrash back and forth having
the power to do stuff....

I want two threads to execute and fight each other to send commands to a
unit. With each thread they just call methods with certain arguments.
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.

To combat the problem I tried doing this after the creation of the
threads, in main I guess you call it...

loop do
a.priority = -1
b.priority = -2
sleep(50)
b.priority = -1
a.priority = -2
end

I also tried positioning Thread.pass randomly throughout the two
threads, hoping that would tell the thread, hey let the other guy have a
chance, then that thread would get to his Thread.pass meaning let the
first guy have a chance to send some stuff!!

All these ways have some outcome, just the second thread executes
forever....

Any ideas, thank you very much for the help!!

========================================
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
1
2

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
17:00:20 desktop$

Kind regards

robert

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

This works for me:

same goes for this:

a = Thread.new do
    loop do
        puts 'a'
        Thread.pass if rand > 0.5
    end
end

b = Thread.new do
    loop do
        puts 'b'
        Thread.pass if rand > 0.5
    end
end

a.join
b.join

if you prefer Thread.pass to sleep.

Greetz!

      m.synchronize do
        puts ii
      end

I appreciate all the help, and it finally worked. i was using 1.8...

Also, on your 1.9 example, what was the point of the mutex synchronize
here, that would only be necessary if they were sharing common data
correct? Not just for printing right... Thanks a lot for everyone's
time.

Matt

···

--
Posted via http://www.ruby-forum.com/\.

That was intended to make it a bit more realistic for the case of using shared resources (the output is a shared resource as well). If you have completely independent operations then using different processes is probably a better option as this will make better use of your system's resources (e.g. multiple CPUs).

Kind regards

  robert

···

On 10/09/2009 10:12 PM, Matt Brooks wrote:

      m.synchronize do
        puts ii
      end

I appreciate all the help, and it finally worked. i was using 1.8...

Also, on your 1.9 example, what was the point of the mutex synchronize here, that would only be necessary if they were sharing common data correct? Not just for printing right... Thanks a lot for everyone's time.

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