简体   繁体   中英

C++ thread/process identifier

是否有一种使用C ++获取线程和/或进程标识符(string,int,...)的可移植方法?

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.

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 (eg 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 (eg 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.

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.

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.

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).

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?

getpid()是一种获取进程ID的可移植方式。

I don't think you're going to find a portable method unless it is through a wrapper library. Each threading system (eg. Windows, or POSIX) are going to have their own mechanism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM