0

Will Thread.CurrentThread.ManagedThreadId return the unique Id on the multiprocessor machine.

Explanation: I want to generate unique number. i generated unique number based on current time i.e. currentTmeinmillisecons but two threads access the same function in same timestamp so i want to add something unique in that timestamp. So planning to to add Thread.CurrentThread.ManagedThreadId in CurrentTimeInMilliseconds.

So can I add Thread.CurrentThread.ManagedThreadId in CurrentTimeInMilliseconds.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Saurabh Mahajan
  • 2,937
  • 7
  • 25
  • 32
  • Please learn formatting your question.. – Soner Gönül Oct 22 '13 at 06:47
  • Sorry for the wrong formmating, I corrected the same. – Saurabh Mahajan Oct 22 '13 at 06:50
  • 2
    If it doesnt need to be a number use `Guid` – Sriram Sakthivel Oct 22 '13 at 06:50
  • GUID is not unique as i seen in "http://stackoverflow.com/questions/1705008/simple-proof-that-guid-is-not-unique" – Saurabh Mahajan Oct 22 '13 at 06:53
  • 2
    @SaurabhMahajan - Do you actually understand the answers to that question? GUID is "unique" to an extent that many other events (such as the extinction of life on Earth) are far more likely to occur before you get an accidental collision. – Damien_The_Unbeliever Oct 22 '13 at 06:57
  • `Thread.CurrentThread.ManagedThreadId` will return a unique id at a given instant in time. But as one thread quits and another one starts the new one might get the old `Id`. Your premise is flawed. – ta.speot.is Oct 22 '13 at 07:07
  • @ta.speot.is reference? It seems obvious that this would be the case (that the thread ID would only be unique among active threads until the thread exits), but I couldn't find it documented anywhere. – Cory Nelson Oct 22 '13 at 07:09
  • The documentation says "unique identifier" but gives no further guarantee. So it's unique for an instant of time. http://rocksolid.gibraltarsoftware.com/development/logging/managed-thread-ids-unique-ids-that-arent-unique – ta.speot.is Oct 22 '13 at 07:11

1 Answers1

3

It's not clear what you mean by "add Thread.CurrentThread.ManagedThreadId in CurrentTimeInMilliseconds". Say, for 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.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • Thanks a lot for ur reply.But actually my method that will return get time in milliseconds was working fine but i faced a issue on multiprocessor machine because , method is called from two threads and both the threads got same timestamp. I want to provide different - different timestamp to all the threads . – Saurabh Mahajan Oct 22 '13 at 07:07
  • *I want to provide different - different timestamp to all the threads* Then call the method at different times. Otherwise what you're given to all the threads is not a timestamp. – ta.speot.is Oct 22 '13 at 07:08
  • Thanks a lot again for the great reply. Actually my method works fine. but i want to update my method only. so how can i generate unique number on multiprocessor machine. So i am doing return currenttimeinMilliseconds + Thread.CurrentThread.ManagedThreadId //unique number So question is : So it will work fine or not on multiprocessor machine. It already works fine in single processor mchine. Actually i am asking for multiprocessor becasuse if Thread.CurrentThread.ManagedThreadId is different for different different threads then my function will return unique number. – Saurabh Mahajan Oct 22 '13 at 07:18
  • I can't sufficiently understand you. If you update your question with code, it'll be easier to help you. – Cory Nelson Oct 22 '13 at 11:47
  • 1
    @CoryNelson `Guid` is not a good choice to create unique number, had tested it http://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time/21570510#21570510 – vikas Apr 30 '14 at 08:34