Green Threads - I want them to thrash...

Discussion in 'Ruby' started by Matt Brooks, Oct 8, 2009.

  1. Matt Brooks

    Matt Brooks Guest

    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/.
    Matt Brooks, Oct 8, 2009
    #1
    1. Advertising

  2. On Fri, Oct 9, 2009 at 12:53 AM, Matt Brooks <> wrote:
    > 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.
    Jesús Gabriel y Galán, Oct 9, 2009
    #2
    1. Advertising

  3. [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!
    Fabian Streitel, Oct 9, 2009
    #3
  4. 2009/10/9 Matt Brooks <>:
    > 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/
    Robert Klemme, Oct 9, 2009
    #4
  5. Matt Brooks

    Matt Brooks Guest


    > 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/.
    Matt Brooks, Oct 9, 2009
    #5
  6. 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.


    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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Oct 10, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Morris [Droopy eyes software]

    Thrash my site

    Peter Morris [Droopy eyes software], Jan 26, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    325
    Juan T. Llibre
    Jan 26, 2005
  2. Naresh Agarwal

    Green threads in Java

    Naresh Agarwal, Jan 9, 2004, in forum: Java
    Replies:
    2
    Views:
    5,509
    Andrew Thompson
    Jan 9, 2004
  3. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    507
    tdavisjr
    Jun 16, 2006
  4. why the lucky stiff
    Replies:
    5
    Views:
    134
    why the lucky stiff
    Sep 22, 2004
  5. Voroztsov Artem
    Replies:
    0
    Views:
    182
    Voroztsov Artem
    Nov 24, 2007
Loading...

Share This Page