2

I am experimenting with multithreading in ruby. I ran this piece of code that runs 3 threads concurrently (ruby threads.rb in my terminal):

arr = []
arr.push(Thread.new do
  1000000.times do |i|
    puts "thread 1"
  end
end)
arr.push(Thread.new do
  1000000.times do |i|
    puts "thread 2"
  end
end)

arr.push(Thread.new do
  1000000.times do |i|
    puts "thread 3"
  end
end)

arr.each {|t| t.join}

I am now running htop in tree view in my terminal to see if I can actually see the 3 different threads :

enter image description here

I think the process threads.rb is the line right under the highlighted one, but I cant see my three launched threads as branches of the threads.rb process.. Do ruby threads have nothing to do with threads and processes displayed with htop ? Is there a way to visualize the different ruby threads running inside my threads.rb process.

David Geismar
  • 3,152
  • 6
  • 41
  • 80

1 Answers1

1

It depends on your Ruby interpreter. MRI should allocate a native thread for each Ruby thread. I've ran your script and I can see threads in htop (there's more than 3 threads, but that must be something the interpreter does):

htop

Try setting a filter in htop (hit F4 and type ruby as a filter string).

EDIT: I've tested on Debian. htop on MacOS apparently doesn't show threads.

eyevan
  • 1,475
  • 8
  • 20
  • What OS and Ruby interpreter are you running? I'm on MacOS 10.14.3 and tested on MRI and don't see native threads. – lacostenycoder May 02 '19 at 14:57
  • nope only see one ruby threads.rb process which seem to be a child from atom. Im running on MRI too with macos 10.14.4 – David Geismar May 02 '19 at 15:52
  • Oh, OK -- I'm on Debian. Seems like `htop` doesn't show threads on OSX: https://github.com/hishamhm/htop/issues/552 -- there's a PR open, but it's not merged. See https://stackoverflow.com/questions/28047653/osx-how-can-i-see-the-tid-of-all-threads-from-my-process, HTH. – eyevan May 02 '19 at 19:33