在 C++ 中获取当前实例(例如 pid)的唯一值的独立于平台的方法是什么?

发布于 2024-12-09 01:57:38 字数 183 浏览 7 评论 0原文

我想以平台无关的方式获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

极度宠爱 2024-12-16 01:57:38

查看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.

爱你不解释 2024-12-16 01:57:38

长话短说,答案是否定的,除非使用特定于平台的功能。

[C++03]

C++ 语言本身没有提供任何机制来执行此操作。事实上,C++ 本身对进程或进程间通信一无所知。不使用特定于平台的功能 - 要么由您手动编写,这意味着 #ifdefs,要么在某些跨平台的第三部分库(如 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 #ifdefs, or in some cross-platform 3rd part library like Boost, which also means #ifdefs -- 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.

记忆之渊 2024-12-16 01:57:38

这可能有点过头了,但请查看 QUuid

This might be an overkill, but take a look into QUuid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文