# Green Threads - I want them to thrash

**URL:** <https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959>\
**Category:** Ruby\
**Created:** [October 9, 2009, 12:53am UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959 "2009-10-09T00:53:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 9, 2009, 12:53am UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959/1 "2009-10-09T00:53:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 9, 2009, 10:24am UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959/2 "2009-10-09T10:24:50Z")

</div>

On Fri, Oct 9, 2009 at 12:53 AM, Matt B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> continously, seemingly randomly to me from the outside.
> 
> 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.

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 9, 2009, 11:56am UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959/3 "2009-10-09T11:56:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 9, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959/4 "2009-10-09T17:06:25Z")

</div>

2009/10/9 Matt B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> continously, seemingly randomly to me from the outside.
> 
> over forever, and thread a never executes again.  
> end  
> 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 = 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

# 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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 9, 2009, 10:12pm UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959/5 "2009-10-09T22:12:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 10, 2009, 11:35am UTC](https://www.ruby-forum.com/t/green-threads-i-want-them-to-thrash/176959/6 "2009-10-10T11:35:38Z")

</div>

On 10/09/2009 10:12 PM, Matt B. wrote:

> 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
