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.
It's possible to generate an identical guid over and over. However, the chances of it happening are so low that you can assume they are unique.
A GUID is a unique number that can be used as an identifier for anything in the universe, but unlike ISBN there is no central authority - the uniqueness of a GUID relies on the algorthm that was used to generate it.
A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.
Definitely not random. Similarly, the person who wanted to use a GUID for password generation would find that the passwords are totally predictable if you know what time the GUID was generated and which computer generated the GUID (which you can get by looking at the final six bytes from some other password-GUID).
Possible (as in, could it ever happen, in the lifetime of the universe)? Yes.
Likely (at all)? No.
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.
Not possible. Static methods of Guid
are guaranteed to be thread-safe. See documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With