Click here to Skip to main content
15,393,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, Actually i need a unique id, so i use the current time with mutex for get that value.
My assumption is that any possible to get a same value at a same nano sec or next year or next month any way . is that any chance please let me know about it.

#include<iostream>
#include<time.h>
using namespace std;

int main()
{
long t1=time(NULL);
cout<<"The value is "<<t1<<endl; this="" value="" will="" leads="" to="" get="" same.="" after="" sometime="" or="" not
}

because="" is="" app="" oriented.="" as="" use="" time="" at="" multi-request.

<b="">What I have tried:

Need to know about the time c-inbuilt function
Posted
Updated 26-Nov-21 15:32pm

Why would you assume that the current time would be unique? It is, but only to the "granularity" of the Tick that updates it - which in a PC can be 1/32th of a second. In this case, a time_t value (which is what the C library function - time()[^] returns) is a number of integral seconds since a defined point in time.

So it you call it twice in the same second, you will get identical results - in fact given the speed of modern processors, you could call it thousands and possibly millions of times and get the same result!

If you want a unique value, see here: CoCreateGuid function (combaseapi.h) - Win32 apps | Microsoft Docs[^]
   
As OriginalGriff has pointed out, the chances of calling the time_t within a single second are quite high. For a point of reference, consider the following program
C++
#include <iostream>
#include <chrono>

using Clock = std::chrono::steady_clock;
using Seconds = std::chrono::seconds;

int main()
{
    const size_t nsec = 2;
    size_t ncalls;
    auto start = Clock::now();
    auto elapsed = [start]() { 
        return std::chrono::duration_cast<Seconds>(Clock::now() - start).count(); };

    for(ncalls = 0; elapsed() < nsec; ++ncalls)
        time(0);

    std::cout << "performed  " << ncalls/nsec << " calls per second\n";
}
Running this on my desktop PC, I get over 23,000,000 calls per second. Even a pi-zero will generate about 375,000 calls per second. This is doing far more than just a single call to time() per loop, so the actual number of system calls is at least 2 times the value printed, perhaps even 3 or 4 times.

So its probably not a good idea to use time() to generate a unique number. You could use the time() call as a seed to the standard C random number generator (srand()/rand()). Or you could look into Pseudo-random number generation - cppreference.com[^]
   
You can use the Boost Uuid Library - 1.77.0[^] The documentation contains sample code, so it's not hard to use.

P.S.: if you don't want to use boost, you can find alternate suggestions with working code here:
How can I generate UUID in c++, without using boost library? - Stack Overflow[^]
The first answer just requires an up-to-date C++ compiler, the second should work in C if you remove the iostream output from the example code, but it will only work on Windows.
   
v2
A compatible C ++ solution like that from k5054 is a good idea. Alternatively, you could use a multimedia timer.
Acquiring high-resolution time stamps - Win32 apps | Microsoft Docs[^]

Note: The time functions from the C library have a significantly higher resolution under Linux.
But this is about Windows, and the resolution of the C standard library is not enough for many things.
   
v2
Quote:
Need to know about the time c-inbuilt function

Obviously you didn't even take the pain to read the documentation, because the documentation tells you what is time() and how it works, so you can deduce tyhe answers to your questions.
time - C++ Reference[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900