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
- Host: GitHub
- URL: https://github.com/tomlobato/ruby-get_tid
- Owner: tomlobato
- Created: 2018-03-18T15:57:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-27T17:47:11.000Z (over 8 years ago)
- Last Synced: 2025-03-20T17:04:28.158Z (over 1 year ago)
- Topics: linux, ruby, threads
- Language: C
- Homepage:
- Size: 10.7 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.