• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

Need a portable version of GetThreadId and GetProcessId to retrieve Process and Thread Id

Hi expert,

I need to retrieve the current  Process and Thread Id information.  GetThreadId and GetProcessId is for windows only?  I need a system call that can portable to all platform.  Does QT has functions to do that?

I am using QT3.3.3.  Thanks!
0
4eyesgirl
Asked:
4eyesgirl
  • 4
  • 4
1 Solution
 
jkrCommented:
There is nothing in common for most platforms, so you'd probably have to write a wrapper around the OS specific calls regarding the process Id, e.g.
#ifdef WIN32
#include <windows.h>
typedef DWORD PID;
#else
#include <sys/types.h>
#include <unistd.h>
typedef pid_t PID;
#endif
 
PID get_process_id() {
#ifdef WIN32
  return GetCurrentProcessId();
#else
  return getpid();
#endif
}

Open in new window

0
 
jkrCommented:
As for the thread Ids - I remember you're using pthreads, you can assign your own Ids like in the sample below. pthreads is portable anyway, so you don't have to care about that.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5
 
void *PrintHello(void *threadid)
{
   int tid;
   tid = (int)threadid;
   printf("Hello World! It's me, thread #%d!\n", tid);
   pthread_exit(NULL);
}
 
int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc, t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %d\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

Open in new window

0
 
4eyesgirlAuthor Commented:
Do you know if QT provides a portable version so I don't have to do the #ifdef
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
No, checked it and found nothing - neither QApplication nor QThread have any members regarding that issue. Anyway, it's just in a single place ;o)
0
 
4eyesgirlAuthor Commented:
Would pthread_self() works?
0
 
jkrCommented:
Well, that gives you the 'pthread_t' for the current thread. So, if that is what you want: Yes.
0
 
4eyesgirlAuthor Commented:
When I do the following, it prints out hex instead of dec

 psthread_t pth_id = psthread_self();
        std::cout << pth_id;

How to convert it to dec on all platforms?
0
 
4eyesgirlAuthor Commented:
can getpid() works on windows?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now