同一进程的两个线程可以产生相同的 GUID 吗?

发布于 2024-09-01 22:44:47 字数 202 浏览 3 评论 0原文

如果进程中的两个线程使用 .NET API (Guid.NewGuid()) 同时生成一个新的 GUID,这两个 GUID 是否可能相同?

谢谢。

更新 我想变得实用。我知道人们普遍认为 GUID 对于所有实际目的都是唯一的。我想知道是否可以以相同的方式处理同一进程的不同线程生成的 GUIDS。

If two threads in a process generate a new GUID concurrently using .NET API (Guid.NewGuid()) is it possible that the two GUIDs will be identical?

Thanks.

UPDATE
I want to get practical. I know that it is widely assumed that GUIDs are unique for all practical purposes. I am wondering if I can treat GUIDS produced by the different threads of the same process in the same manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

番薯 2024-09-08 22:44:47

简短回答

可能(例如,在宇宙的一生中,它曾经会发生吗)?是的。

可能(全部)?否。


更长的答案

Microsoft 使用 版本 4 算法 来生成 GUID(另请参阅:此处),它产生一个完全(伪)随机数。

鉴于可能的 GUID 数量,重复的概率很小。就像,小得不可思议

您关心并发性:幸运的是,NewGuid 方法是 线程安全,这意味着它要么锁定要么利用线程静态随机数生成器来实现其目的。第一种方法将有效地序列化对 NewGuid 的所有调用,以便它们按顺序发生(绝不同时),而后者将从彼此独立的单独线程进行调用。

无论哪种情况,您必须担心从同时创建随机数的两个线程中获取重复项(无论是否为 GUID),是线程使用的底层生成器是否正在运行 (1)来自相同的种子(这只能是由于设计缺陷造成的), (2) 以时间相关的方式(版本 4 GUID 算法则不然)。

所以,是的,实际上,您可以将单独线程同时生成的 GUID 视为唯一的。

Short Answer

Possible (as in, could it ever happen, in the lifetime of the universe)? Yes.

Likely (at all)? No.


Longer Answer

Microsoft utilizes a Version 4 algorithm for generating GUIDs (see also: here), which produces a completely (pseudo-)random number.

Given the number of possible GUIDs, the probability of a duplicate is tiny. Like, unfathomably tiny.

You are concerned with concurrency: fortunately, the NewGuid method is thread-safe, which means it either locks or utilizes a thread-static random number generator for its purposes. The first approach would effectively serialize all calls to NewGuid so that they occur in sequence (never simultaneously) while the latter would make calls from separate threads independent of one another.

In either case, the only reason you would have to fear getting duplicates from two threads creating random numbers simultaneously -- GUID or not -- would be if the underlying generators used by the threads were operating (1) from the same seed (which could only result from a design flaw), and (2) in a time-dependent manner (which the version 4 GUID algorithm does not).

So yes, practically speaking, you can treat GUIDs generated concurrently from separate threads to be unique.

夏雨凉 2024-09-08 22:44:47

不可能。 Guid 的静态方法保证是线程安全的。请参阅此处的文档。

Not possible. Static methods of Guid are guaranteed to be thread-safe. See documentation here.

三五鸿雁 2024-09-08 22:44:47

这不太可能发生...

http ://msdn.microsoft.com/en-gb/library/system.guid(v=VS.95).aspx

此类型的任何公共静态(在 Visual Basic 中为共享)成员都是线程安全的。不保证任何实例成员都是线程安全的。[...]

GUID 是一个 128 位整数(16 字节)
可以在所有计算机上使用
和网络,只要有独特的地方
需要标识符。这样一个
标识符的概率非常低
被重复。

It's not likely to happen...

http://msdn.microsoft.com/en-gb/library/system.guid(v=VS.95).aspx

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.[...]

A GUID is a 128-bit integer (16 bytes)
that can be used across all computers
and networks wherever a unique
identifier is required. Such an
identifier has a very low probability
of being duplicated.

赴月观长安 2024-09-08 22:44:47

好吧,.Net 的当前实现使用 CoCreateGuid内部:

可以高度确定的是,该函数返回一个唯一的值 - 在同一系统或任何其他系统(无论是否联网)上的其他调用都不应返回相同的值。

Well, current implementations of .Net use CoCreateGuid internally:

To a very high degree of certainty, this function returns a unique value – no other invocation, on the same or any other system (networked or not), should return the same value.

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