[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2008-06-24
8
Medium Priority
?
860 Views
Last Modified: 2011-09-20
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
Comment
Question by:4eyesgirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 21858889
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
 
LVL 86

Expert Comment

by:jkr
ID: 21858920
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
 

Author Comment

by:4eyesgirl
ID: 21860612
Do you know if QT provides a portable version so I don't have to do the #ifdef
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 21860772
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
 

Author Comment

by:4eyesgirl
ID: 21867683
Would pthread_self() works?
0
 
LVL 86

Expert Comment

by:jkr
ID: 21867733
Well, that gives you the 'pthread_t' for the current thread. So, if that is what you want: Yes.
0
 

Author Comment

by:4eyesgirl
ID: 21868285
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
 

Author Comment

by:4eyesgirl
ID: 21868464
can getpid() works on windows?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question