carb::this_process::getUniqueId

Defined in carb/process/Util.h

inline uint64_t carb::this_process::getUniqueId()

Returns an ID uniquely identifying this process at least for the uptime of the machine.

Process IDs aren’t unique; they can be reused. They are great at identifying a process at a given point in time, but not on a timeline that includes the future and the past. That’s what this function seeks to do: give an ultra-high probability that the generated ID has never been in use on this system since the last restart.

This function accomplishes this by combining PID with process creation time.

On Windows, 30 bits are available for process IDs and the remaining 34 bits are used for the timestamp in 32ms units. For a collision to happen, either 17.4 years would have to pass for rollover or a process would have to start, finish and the process ID be reused by the system within the same 32ms unit.

For Linux, up to 22 bits are available for process IDs (but most systems use the default of 15 bits). The remaining 42 bits are used for the timestamp. The timestamp is based on the kernel frequency (ticks per second). A kernel frequency of 100 will result in using 10ms units for the timestamp and the rollover period is 1,394 years. The maximum resolution used for the timer will result in using 1 ms units for the timestamp and a rollover period of 139.4 years. For a collision to happen, either the rollover period would have to pass or a process would have to start, finish and the process ID be reused by the system within the timestamp unit (1 ms - 10 ms).

Note

The first call to this function within a module may be slow as additional information is obtained from the system. That information is then cached and subsequent calls within the module are very fast.

Warning

This function is frozen to maintain ABI compatibility over plugins that may be built at different times. Do not change the results of this function, ever! Instead, add a different function.

Returns

A unique identifier for this process from the last restart until the system is rebooted.