views:

806

answers:

7

Is there a portable way of getting thread and/or process identifier (string, int, ...) with C++?

+5  A: 

You have a few ways, but all imply the use of an external library abstracting the thread for you.

Among the popular choices, two are:

  1. The Boost.Thread library. This is the most portable but imply working with Boost, which is a HUGE library
  2. The Qt library. This is less portable and imply to work with Qt, a big library.

If you already use any on these two libraries, I would recommend sticking with it. Otherwise, look at what other tools they provide and make a choice.

PierreBdR
Boost is pretty big, but keep in mind that you only pay for what you use.
Ferruccio
+1  A: 

I always thought threads were external from C++. In Java, there's a native thread built-in to the language.

You'd have to find a portable thread library.

Robert
A: 

I'm not sure how portable they are but Posix threads may be another option you want consider. See also here. I agree with Steve's comment--portable to which platforms?

Onorio Catenacci
+1  A: 

The only way is to use portable library. Id recommend Qt (it doesn't have to be GUI app) or maybe wxWidgets. If you are developing a game check out SDL

Also check out boost libs they might have something.

Luka Marinko
+1 for SDL.....
Robert Karl
+4  A: 

There is no portable way when portable means a way that works on every platform for that a C++ compiler exists. Such a way had to be part of the C++ standard, in which case it really would work everywhere (just like the other parts of the C++ standard work everywhere). Everything not in the standard is not guaranteed to work on any platform, unless the platform states to support this standard.

Every solution people have suggested here is a solution that uses an external library and thus can only work on platforms supported by that library; and no library is available for every existing platform.

Probably the one that will get you farthest is POSIX, after all every UNIX like system tries to support at least some POSIX (the more the better), very little can call themselves really being 100% POSIX-compliant platforms (e.g. A/UX, AIX, HP-UX, IRIX, Mac OS X 10.5, MINIX, QNX, Solaris, UnixWare, VxWorks, ... to name a few, there are more of course). However, there are quite a couple of platforms that offer at least some POSIX support, some more, some less and some are almost POSIX-compliant (e.g. FreeBSD, Linux, NetBSD, BeOS, OpenBSD, ... and others).

Windows is unfortunately far away from being one. NT used to be partly POSIX conform, but now it has more or less vanished (Win2000/20003, WinXP, and Vista can still be set into a POSIX emulating mode, translating some POSIX call to internal API calls by installing Microsoft Windows Services for UNIX - SFU 3.5 or higher), however there are ways to get some POSIX functionality on Windows through external libraries as well (Cygwin offers LGPL libraries you can link with your app to enable a fair amount of POSIX functions on Windows).

The advantage of POSIX is not only that it is relatively widespread, but also that it is standardized and you can easily look up the standard on the Internet. Using POSIX calls you can get a thread id and a process id.

Mecki
A: 

getpid() is a portable way to get the process ID.

Lev
+1  A: 

You may also use part of ACE library, which implements a platform independent wrapper. Finding PID would be one of the files in the library (maybe ACE_Process/ ACE_Thread).

Amit Kumar
If your intent is to get the platform Process IDs in a portable way then ACE is a proven solution for many platforms. However not every platform may provide the same information. In that case you won't get what you want.
lothar