简体   繁体   English

Thread.CurrentThread.ManagedThreadId多处理器是否安全

[英]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.

相关问题 Thread.CurrentThread.ManagedThreadId和AppDomain.GetCurrentThreadId()之间的区别 - Difference between Thread.CurrentThread.ManagedThreadId and AppDomain.GetCurrentThreadId() c# Thread.Equals 与 Thread.CurrentThread.ManagedThreadId 比较 - c# Thread.Equals vs Thread.CurrentThread.ManagedThreadId comparison 重复调用CurrentThread.ManagedThreadId或将值存储在线程本地存储中? - Repeatedly call CurrentThread.ManagedThreadId or store the value in thread local storage? 如何在Visual Studio中启用多处理器构建 - How do I enable multi-processor builds in Visual Studio 如何以编程方式确定是在多核,超线程还是在多处理器上? - How can I determine programmatically whether on multi-core, hyperthreading or multi-processor? C#:将作业分配给多处理器计算机上的工作进程 - C#: Farm out jobs to worker processes on a multi-processor machine 保证多处理器系统上的多个线程始终可以看到变量的最新值 - guarantee that up-to-date value of variable is always visible to several threads on multi-processor system 多线程安全计数器 - Multi-Thread Safe Counters 使用多核(-thread)处理器进行FOR循环 - Using Multi-core (-thread) processor for FOR loop 多线程环境中的线程安全方法 - Thread safe method in multi threaded environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM