Skip to content

tomlobato/ruby-get_tid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ruby-get_tid

man 2 gettid:

"gettid() returns the caller's thread ID (TID).
In a single-threaded process, the thread ID is equal to the process ID (PID, as returned by getpid(2)).
In a multithreaded process, all threads have the same PID, but each one has a unique TID."

Why?

While Thread.current.object_id is great for internal thread identification, external tools (eg.: monitors) needs an OS identifier for them, for instance to get the resources stats of a specific thread.

Compile

ruby extconf.rb
make

Test

test.rb:

require './thread_info'
include ThreadInfo

puts "Main proccess PID: #{Process.pid}"
Thread.new{
    puts "Thread TID: #{get_tid}"
    puts `ps axH -o pid,pgid,tid,comm,args -q #{$$}`
}.join
tom@vm1:~/ruby-get_tid# ./test.rb
Main proccess PID: 382
Thread TID: 411
  PID  PGID   TID COMMAND         COMMAND
  382   382   382 ruby            ruby ./test.rb
  382   382   410 ruby-timer-thr  ruby ./test.rb
  382   382   411 test.rb:7       ruby ./test.rb

TODO

  • Portability: currently working only on Linux.
  • Move get_tid method to the Process class.
  • Turn into a gem.

About

Get the Thread ID (tid/lwp/spid) in Ruby/Linux

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published