在 C++ 中生成唯一 ID 的算法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1988679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Algorithm for generating a unique ID in C++?
提问by Ajay
What can be the best algorithm to generate a unique id in C++? The length ID should be a 32 bit unsigned integer.
在 C++ 中生成唯一 ID 的最佳算法是什么?长度 ID 应该是一个 32 位无符号整数。
回答by Hans Passant
Getting a unique 32-bit ID is intuitively simple: the next one. Works 4 billion times. Unique for 136 years if you need one a second. The devil is in the detail: what was the previous one? You need a reliable way to persist the last used value and an atomic way to update it.
获得唯一的 32 位 ID 直观上很简单:下一个。工作40亿次。136 年来独一无二,如果您每秒需要一个。魔鬼在细节:前一个是什么?您需要一种可靠的方式来持久化上次使用的值,并需要一种原子方式来更新它。
How hard that will be depends on the scope of the ID. If it is one thread in one process then you only need a file. If it is multiple threads in one process then you need a file and a mutex. If is multiple processes on one machine then you need a file and a named mutex. If it is multiple processes on multiple machines then you need to assign a authoritative ID provider, a single server that all machines talk to. A database engine is a common provider like that, they have this built-in as a feature, an auto-increment column.
难度取决于 ID 的范围。如果它是一个进程中的一个线程,那么您只需要一个文件。如果一个进程中有多个线程,那么您需要一个文件和一个互斥锁。如果一台机器上有多个进程,那么您需要一个文件和一个命名的互斥锁。如果是多台机器上的多个进程,那么你需要分配一个权威的 ID 提供者,一个所有机器都与之通信的服务器。数据库引擎是一个常见的提供者,他们有这个内置的特性,一个自动增量列。
The expense of getting the ID goes progressively up as the scope widens. When it becomes impractical, scope is Internet or provider too slow or unavailable then you need to give up on a 32-bit value. Switch to a random value. One that's random enough to make the likelihood that the machine is struck by a meteor is at least a million times more likely than repeating the same ID. A goo-ID. It is only 4 times as large.
随着范围的扩大,获取 ID 的费用会逐渐增加。当它变得不切实际,范围是 Internet 或提供商太慢或不可用时,您需要放弃 32 位值。切换到随机值。一个足够随机以使得机器被流星撞击的可能性比重复相同 ID 的可能性至少高一百万倍。一个粘性 ID。它只有 4 倍大。
回答by jalf
Here's the simplest ID I can think of.
这是我能想到的最简单的 ID。
MyObject obj;
uint32_t id = reinterpret_cast<uint32_t>(&obj);
At any given time, this ID will be unique across the application. No other object will be located at the same address. Of course, if you restart the application, the object may be assigned a new ID. And once the object's lifetime ends, another object may be assigned the same ID.
在任何给定时间,此 ID 在整个应用程序中都是唯一的。没有其他对象将位于同一地址。当然,如果您重新启动应用程序,对象可能会被分配一个新的 ID。一旦对象的生命周期结束,另一个对象可能会被分配相同的 ID。
And objects in different memory spaces (say, on different computers) may be assigned identical IDs.
不同内存空间(例如,在不同计算机上)中的对象可能会被分配相同的 ID。
And last but not least, if the pointer size is larger than 32 bits, the mapping will not be unique.
最后但并非最不重要的一点是,如果指针大小大于 32 位,则映射将不是唯一的。
But since we know nothing about what kind of ID you want, and how unique it should be, this seems as good an answer as any.
但是由于我们对您想要什么样的 ID 以及它应该是多么独特一无所知,所以这似乎是一个很好的答案。
回答by Sajad Bahmani
You can see this. (Complete answer, I think, is on Stack Overflow.)
Some note for unique id in C++ in Linux in this site. And you can use uuid in Linux, see this man page and samplefor this.
你可以看到这个。(我认为完整的答案在 Stack Overflow 上。)此站点
中 Linux 中 C++ 中唯一 id 的一些注释。您可以在 Linux 中使用 uuid,请参阅此手册页和示例。
If you use windows and need windows APIs, see this MSDN page.
如果您使用 Windows 并需要 Windows API,请参阅此MSDN 页面。
This Wikipedia page is also useful: http://en.wikipedia.org/wiki/Universally_Unique_Identifier.
这个维基百科页面也很有用:http: //en.wikipedia.org/wiki/Universally_Unique_Identifier。
回答by an0nym0usc0ward
DWORD uid = ::GetTickCount();
::Sleep(100);
回答by Sahib Yar
If you can afford to use Boost, then there is a UUIDlibrary that should do the trick. It's very straightforward to use - check the documentation and this answer.
回答by ezpz
There is little context, but if you are looking for a unique ID for objects within your application you can always use a singleton approach similar to
几乎没有上下文,但是如果您正在为应用程序中的对象寻找唯一 ID,您始终可以使用类似于以下内容的单例方法
class IDGenerator {
public:
static IDGenerator * instance ();
uint32_t next () { return _id++; }
private:
IDGenerator () : _id(0) {}
static IDGenerator * only_copy;
uint32_t _id;
}
IDGenerator *
IDGenerator::instance () {
if (!only_copy) {
only_copy = new IDGenerator();
}
return only_copy;
}
And now you can get a unique ID at any time by doing:
现在您可以随时通过以下方式获得唯一 ID:
IDGenerator::instance()->next ()
IDGenerator::instance()->next ()