在 C++ 中获取当前实例(例如 pid)的唯一值的独立于平台的方法是什么?
我想以平台无关的方式获取C++中当前实例的某种唯一整数值(无论它是进程ID还是其他任何东西)(这意味着没有#if #否则宏开关)。
基于时间的值不可靠,因为两个实例可以同时启动。
有什么办法可以实现这一点吗?
编辑:它不必是全球唯一的。它只需要在系统范围内是唯一的。
I would like to get a some kind (doesn't matter whether it's a process id or anything) of a unique integer value of current instance in C++ in a platform independent manner (that means no #if #else macro switch).
time based value would not be reliable since two instances can start at the same time.
Is there any way of achieving this?
EDIT: It doesn't have to be globally unique. it just needs to be unique system wide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看Boost 流程。可能正是您正在寻找的东西。如果您不想包含该库,可以看看该功能是如何实现的。
Take a look at Boost process. Might be exactly what you're looking for. If you don't want to include the library, you can take a look at how the functionality is implemented.
长话短说,答案是否定的,除非使用特定于平台的功能。
[C++03]
C++ 语言本身没有提供任何机制来执行此操作。事实上,C++ 本身对进程或进程间通信一无所知。不使用特定于平台的功能 - 要么由您手动编写,这意味着
#ifdef
s,要么在某些跨平台的第三部分库(如 Boost)中,这也意味着#ifdef
code>s——这是无法完成的。...并且...
[C++11]
C++ 语言有线程的基本概念,但它没有提供(我可以看到的)创建进程间通信机制的机制,因此上述内容仍然适用。
Long story short, the answer is no, not without using platform-specific functionality.
[C++03]
The C++ language itself does not provide any mechanism to do this. In fact, C++ itself knows nothing about processes or interprocess communication. Without using platform-specific functionality -- either hand-rolled by you, which means
#ifdef
s, or in some cross-platform 3rd part library like Boost, which also means#ifdef
s -- this cannot be done....and...
[C++11]
The C++ language has a basic concept of threads, but it provides no mechanism (that I can see) to create an interprocess communication mechanism, so the above still applies.
这可能有点过头了,但请查看 QUuid
This might be an overkill, but take a look into QUuid
我认为,您需要通用唯一标识符
I think, you need Universally unique identifier