tags:

views:

126

answers:

4

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?

+3  A: 

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.

Lasse V. Karlsen
See, that's what I assumed but haven't seen any mention of 'serial number inside' anywhere - have you got a reference?
billybum
Unfortunately I don't have a link to the article I read that mentioned this serial number, but if you look at the documentation for UuidCreate, the core function that produces Guids in Windows (http://msdn.microsoft.com/en-us/library/Aa379205), you'll see that it says that "The UUID is guaranteed to be unique to this computer only.". Obviously it cannot make such guarantees if there isn't at least one thing that changes for each call.
Lasse V. Karlsen
Thanks, but that's still not totally conclusive...
billybum
An Uuid is supposed to be a black box, so I doubt you'll find more conclusive information from Microsoft on how they generate these, but the fact that it is documented that it is unique should be enough. Isn't it?
Lasse V. Karlsen
This is the fullest account of UUID generation that I've found: http://www.ietf.org/rfc/rfc4122.txtI can't see anything specific here but there is mention of the situation where many UIDs are requested per minimum time interval, which is the case I'm interested in. It all depends how .NET implements the algorithm - the statement you refer to is related but not definitive as far as I'm concerned.
billybum
Believe me, UUID creation is threadsafe on Windows and the developers have thought about it. I used to be a developer in the Windows division at Microsoft and I've seen the source.UuidCreateSequential basically follows section 4.2 of RFC4122; UuidCreate follows section 4.4 (random). Guid.NewGuid is built on top of UuidCreate.
George V. Reilly
A: 

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.

anishmarokey
A: 

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.

apoorv020
I don't think that multicore is so absurd these days so I would say it is pretty much a guarantee that todays code will run on multiple cores at some point.
Lasse V. Karlsen
Yes, but usually a process is run on only a single core(because threads share memory and transferring memory between cores running different threads of same process is expensive.)At least, that is what I remember from my OS course a year back. I may be wrong on this point.
apoorv020
For cache locality, the scheduler will typically prefer to keep threads from a process on the same core(s). But this is hardly guaranteed. The only runnable threads may all be from the same process. This is quite common in server apps.Regardless of the vagaries of the scheduler, you could have GUIDs being created from multiple processes simultaneously.
George V. Reilly
+1  A: 

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)

George V. Reilly