[英]Is Thread.CurrentThread.ManagedThreadId multi-processor safe
Will Thread.CurrentThread.ManagedThreadId
return the unique Id on the multiprocessor machine. Thread.CurrentThread.ManagedThreadId
将返回多处理器计算机上的唯一ID。
Explanation: I want to generate unique number. 说明:我要生成唯一编号。 i generated unique number based on current time ie currentTmeinmillisecons but two threads access the same function in same timestamp so i want to add something unique in that timestamp.
我根据当前时间(即currentTmeinmillisecons)生成了唯一编号,但是两个线程在同一时间戳中访问了相同的函数,因此我想在该时间戳中添加唯一的内容。 So planning to to add
Thread.CurrentThread.ManagedThreadId
in CurrentTimeInMilliseconds. 因此,计划在CurrentTimeInMilliseconds中添加
Thread.CurrentThread.ManagedThreadId
。
So can I add Thread.CurrentThread.ManagedThreadId in CurrentTimeInMilliseconds. 所以我可以在CurrentTimeInMilliseconds中添加Thread.CurrentThread.ManagedThreadId。
It's not clear what you mean by "add Thread.CurrentThread.ManagedThreadId in CurrentTimeInMilliseconds". 不清楚“在CurrentTimeInMilliseconds中添加Thread.CurrentThread.ManagedThreadId”是什么意思。 Say, for ManagedThreadId+CurrentTimeInMilliseconds:
说,对于ManagedThreadId + CurrentTimeInMilliseconds:
1+2 = 3
2+1 = 3
So are you actually creating a "unique" value in the end? 那么,您实际上是在最后创建一个“唯一”值吗? What happens if you call it more than once per millisecond?
如果您每毫秒调用一次以上会怎样? If you need something that will be unique per-process per-call, I'd do something like:
如果您需要每个呼叫每个进程唯一的东西,我可以做以下事情:
using System.Threading;
static class Foo
{
static long i;
public static long GetUnique() { return Interlocked.Increment(ref i); }
}
If you want to generate a unique number over multiple processes, Guid
is the way to go. 如果要在多个过程中生成唯一编号,则可以使用
Guid
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.