0

I'm using boost::thread (which is in a next step using pthread) but would like to get the thread id / (lightweight) pid by the boost::thread pointer. I manage to do this with windows by using the member native_handle() but I couldn't find an alternative with linux.

boost::thread * boost_thread ptr = new boost::thread(/*...*/);
ptr->native_handle();

I've found a lot of posts about the current thread using something like pthread_self() but I'm no trying to get the thread id from inside this thread, I want to get it from the outside for the thread I just created using new boost::thread. Is there a way to retrieve the thread id/pid by using the native handle or something from the boost::thread structure?

Thank you very much!

EDIT: Not a duplicate cause the other thread doesn't solve this... at least not when getting the pthread_t from a boost::thread and thus having no influence upon it's creation. Or solved in a way like: not possible.

Hhut
  • 1,128
  • 1
  • 12
  • 24
  • `boost::thread::native_handle()` on linux returns the `pthread_t` for the thread. Thus, your question is actually a duplicate. – jxh Aug 06 '14 at 07:46
  • What are you needing the linux thread id for? You use boost::thread, so you aren't apparently implementing your own thread library, so why do you need to access kernel details? – eerorika Aug 06 '14 at 08:11
  • So if I understand the other thread correctly it's not possible? – Hhut Aug 06 '14 at 15:26
  • @user2079303: I want this for debugging purposes. I want to see the results in htop and compare to my logs while using linux the same way as currently in windows. But for that I need to compare my internal thread view with external ones (just like htop for example) – Hhut Aug 06 '14 at 15:28
  • I check the definition of ``pthread_t`` and it seems it won't get me anywhere in the short run. These fields doesn't seem to have what I need: ``void * handle; void *(* func )(void *arg); void * arg; void * ret;`` – Hhut Aug 06 '14 at 15:35

3 Answers3

1

The easiest solution is to call getpid() directly from the thread getpid(). (Please do not forget, that in Linux process and thread is almost the same, thus you need the process ID).

Xoranus
  • 53
  • 1
  • 6
  • 1
    In multithreaded programs this is sadly not the same. – Stefan Weiser Aug 06 '14 at 07:44
  • It's probably possible to extend ``boost::thread`` and wrap the calling convention to include ``getpid()`` just after creation for multiple platforms and provide it to the outside world – Hhut Aug 06 '14 at 15:32
1

On most unixoids pid_t current_thread_id = gettid(); will return the current thread id, but glibc does not provide this function. Instead you have to call:

pid_t current_thread_id = (pid_t) syscall(__NR_gettid);

Getting the id from outside that thread will become difficult. Using the native handle of pthread is an unsupported way of getting thread ids. Using boost::thread::get_id() will return an object, so it is not lightweight.

Stefan Weiser
  • 2,264
  • 16
  • 25
  • Thanks... I didn't want to try to access private object data, but the private implementation details of ``boost::thread::id`` do include the id... at least in windows. – Hhut Aug 06 '14 at 15:39
0

On Linux I am able to get thread id using boost::this_thread::get_id()

#include <iostream>
#include <boost/thread.hpp>


void func(){

  std::cout<<"Thrd Id "<<boost::this_thread::get_id()<<" "<<std::endl;
}


int main(){

  boost::thread thd1(&func);

  std::cout<<std::endl;
  boost::thread thd2(&func);

  while(1){}

  return 0;
}

Output:

Thrd Id 7f8ddcef8700
Thrd Id 7f8dd44f7700
Nik
  • 1,294
  • 10
  • 16