Question

As GUID generation is time-dependent, if System.Guid.NewGuid() is called multiple times at the exact same instant on different threads, could it return identical GUIDs?

Was it helpful?

Solution

On Windows, GUIDs (UUIDs) are created from a cryptographic random number generator with UuidCreate. They are version 4 UUIDs in terms of RFC 4122. No timestamps or ethernet cards are involved, unless you're using old school version 1 GUIDs created with UuidCreateSequential.

See also How Random is System.Guid.NewGuid()? (Take two)

OTHER TIPS

No, there is a serial number inside it that changes for each call, so multiple simultaneous calls on different threads on the same system will not create duplicate Guids.

That does not mean that there is a visible part of the Guid that you can see increments for each call.

The Transact-SQL NEWID function and the application API functions and methods generate new uniqueidentifier values from the identification number of their network card plus a unique number from the CPU clock. Each network card has a unique identification number. The uniqueidentifier value that is returned by NEWID is generated by using the network card on the server. The uniqueidentifier value returned by application API functions and methods is generated by using the network card on the client.

.It wont generate duplicates in time or in any pc as per msdn.

Unless your threads are running on different cores on your machine, only one thread will be actually running at a given time. Hence, I do not think it is very likely that calls to NewGuid() will take place at the exact same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top