views:

349

answers:

7

I am using a static library; it has a function which uses the current time and creates a unique id, which is then inserted into my database. This number should be unique in my database table.

There are two processes running in parallel. Sometimes they simultaneously call this function, and the same number is generated. I get an integrity violation when this happens.

I am thinking to use the process id, the thread id, and the current time. Is this combination unique?

Platform: Windows XP

+9  A: 

Use the database to generate them. How to do that depends on the database, but Postgres calls them sequences for an example.

Roger Pate
+3  A: 

The process/thread id will be unique if the programs are running simultaneously as the OS needs to differentiate them. But the system does reuse ids. So, for your situation, yes, its a good idea to add either process id or thread id into your marker, tho I don't think you'd need both.

Joel Lucsy
A: 

process*n+thread_id (in which n is the total number of threads)

csiz
+1  A: 

Whilst the process id and thread id will be unique it would be better to use the database to generate the unique id for you (as R. Pate suggests) if only because you're potentially limiting your scalability unless you also include a unique machine id as well...

Though it's probably reasonably unlikely that one of your processes running on machine A will have the same process id and thread id as one of your processes running on machine B those are always the kinds of bugs that end up getting people out of bed at 4am to deal with the support call...

Len Holgate
A: 

Well, adding process id and thread id could possibly lead to the same number

pid= 100, tid= 104 pid= 108, tid= 96

Not quite likely but possible.

So for near safe ids, you'll need at least a 64 bit ID field like

  ULONG64 id = ((ULONG64)(pid&0xffff) << 48) | ((ULONG64)(tid&0xffff) << 32) | (timestamp & 0xffffffff);

(however, this still does not guarantee uniqueness as it assumes that thread ids don't overlap in a way with process ids that they neutralize 16-bit values, but I don't think I ever saw PIDs over 65536 and unless you are creating thousands of threads, the thread IDs will not wrap around in this value before the timestamp jumps).

Nicholaz
+1  A: 

I think thread_ids from different processes can be the same? (ie only unique per process).

How about using GUIDs?

tony
+2  A: 

The combination of process ID, thread ID and time is unfortunately not guaranteed to be unique. The OS may reuse process IDs and thread IDs once the threads and processes they referred to have terminated. Also, the user may set the clock back, so the same time occurs twice. As others have said, I'd ask the database for a unique ID. Oracle has sequences, MySQL has auto-increment columns, other databases have similar mechanisms.

Anthony Williams