An open API service indexing awesome lists of open source software.

https://github.com/tomlobato/ruby-get_tid

Get the Thread ID (tid/lwp/spid) in Ruby/Linux
https://github.com/tomlobato/ruby-get_tid

linux ruby threads

Last synced: over 1 year ago
JSON representation

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

Awesome Lists containing this project

README

          

# 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
```bash
ruby extconf.rb
make
```

## Test

test.rb:
```ruby
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
```

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