Green Threads - I want them to thrash...

M

Matt Brooks

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!!
 
J

Jesús Gabriel y Galán

All Day I have tried to accomplish the following using threads, and just
could not get it to work. =A0I 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. =A0With 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 =3D Thread.new do
loop do
method that sends commands, about 60 of them
end
end


b =3D 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 =3D Thread.new do
10.times do
puts "command from A"
sleep rand
end
end

b =3D 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.
 
F

Fabian Streitel

[Note: parts of this message were removed to make it a legal post.]
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!
 
R

Robert Klemme

2009/10/9 Matt Brooks said:
All Day I have tried to accomplish the following using threads, and just
could not get it to work. =A0I 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. =A0With 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 =3D Thread.new do
loop do
method that sends commands, about 60 of them
end
end


b =3D 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...

=A0loop do
=A0a.priority =3D -1
=A0b.priority =3D -2
=A0sleep(50)
=A0b.priority =3D -1
=A0a.priority =3D -2
=A0end


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!!

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 =3D 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
1
2
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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

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

Matt Brooks

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
 
R

Robert Klemme

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.

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
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top