windows 线程和进程 ID 是否唯一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1749740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are thread and process ids unique?
提问by Satbir
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.
我正在使用静态库;它有一个函数,它使用当前时间并创建一个唯一的 id,然后将其插入到我的数据库中。这个数字在我的数据库表中应该是唯一的。
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?
我正在考虑使用进程 id、线程 id 和当前时间。这个组合是独一无二的吗?
Platform: Windows XP
平台:Windows XP
回答by
Use the database to generate them. How to do that depends on the database, but Postgres calls them sequences for an example.
使用数据库生成它们。如何做到这一点取决于数据库,但 Postgres 将它们称为序列作为示例。
回答by Joel Lucsy
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.
如果程序同时运行,则进程/线程 id 将是唯一的,因为操作系统需要区分它们。但是系统确实会重用 ID。因此,对于您的情况,是的,将进程 id 或线程 id 添加到标记中是个好主意,但我认为您不需要两者。
回答by David Burg
On Windows, thread Ids are unique throughout the system. See this MSDN library article:
在 Windows 上,线程 ID 在整个系统中是唯一的。请参阅此 MSDN 库文章:
http://msdn.microsoft.com/en-us/library/ms686746%28v=VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms686746%28v=VS.85%29.aspx
The CreateThread and CreateRemoteThread functions also return an identifier that uniquely identifies the thread throughout the system. A thread can use the GetCurrentThreadId function to get its own thread identifier. The identifiers are valid from the time the thread is created until the thread has been terminated. Note that no thread identifier will ever be 0.
CreateThread 和 CreateRemoteThread 函数还返回一个标识符,该标识符在整个系统中唯一标识线程。线程可以使用 GetCurrentThreadId 函数来获取自己的线程标识符。标识符从线程创建之时起一直有效,直到线程终止。请注意,任何线程标识符都不会为 0。
回答by Anthony Williams
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.
不幸的是,进程 ID、线程 ID 和时间的组合不能保证是唯一的。一旦它们引用的线程和进程终止,操作系统可能会重用进程 ID 和线程 ID。此外,用户可以将时钟调回,因此相同的时间会出现两次。正如其他人所说,我会要求数据库提供一个唯一的 ID。Oracle 有序列,MySQL 有自增列,其他数据库也有类似的机制。
回答by Len Holgate
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...
虽然进程 id 和线程 id 将是唯一的,但最好使用数据库为您生成唯一的 id(如 R. Pate 建议的那样),因为您可能会限制可扩展性,除非您还包含唯一的机器 id还有……
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...
尽管在机器 A 上运行的一个进程与在机器 B 上运行的进程之一具有相同的进程 ID 和线程 ID 可能不太可能,但这些总是导致人们在凌晨 4 点起床的错误类型处理支持电话...
回答by Nicholaz
Well, adding process id and thread id could possibly lead to the same number
那么,添加进程 id 和线程 id 可能会导致相同的数字
pid= 100, tid= 104 pid= 108, tid= 96
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
因此,对于接近安全的 ID,您至少需要一个 64 位 ID 字段,例如
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).
(但是,这仍然不能保证唯一性,因为它假设线程 ID 不会与它们中和 16 位值的进程 ID 重叠,但我认为我从未见过超过 65536 的 PID,除非您正在创建数以千计的线程,线程 ID 不会在时间戳跳转之前环绕在此值中)。