Search code examples
c++platform-independent

What is a platform independent way of getting unique value of current instance (e.g. pid) in C++


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.


Solution

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