c++ - Is there a time function its result is guaranteed to change every time is called? -


i tought gettickcount64() until tried this:

#include <fstream> #include <windows.h> #include <iostream>  void dosomething();  int main() { srand(gettickcount64()); std::fstream file; file.open("test.dat", std::ios_base::out | std::ios_base::trunc);      (;;)         file <<std::boolalpha<<(gettickcount64()==gettickcount64()) << std::endl; } 

and this:

#include <fstream> #include <windows.h> #include <iostream>  void dosomething();  int main() { srand(gettickcount64()); std::fstream file; file.open("test.dat", std::ios_base::out | std::ios_base::trunc);  __int64 a, b; (;;) {     = gettickcount64();     dosomething();     b = gettickcount64();     file <<std::boolalpha<<(a==b) << std::endl;     } }  void dosomething() { int t=(see below);     (int = 0; < (rand() % t); i++)         __noop; } 

with 1st variant received 1 false every 200.000 true (806642 iterations).

with second variant , t=5 received 1 false every 90000 true (627018 iterations), t=1000 received 1 false every 4444 true (693361 iterations).

so absolutely not giving different values every time called. need create timestamp in shared memory area, , had lot of fun trying guess why program wasn't working!

what function can use instead? thanks!

edit clarify: understand timer can't have infinite accuracy, wondering if exists timer accurate enough faster cpu (yes, it's absurd question have vague idea how timers works).

btw you're right, i'll use random function generate unique number , i'll store time in place.

since care value being monotonically increasing, , don't (apparently) care how difference there between 1 , next, seems interlockedincrement64 work you.

from there it's simple matter of ensuring code needs serial number gets access same location. windows has couple of ways of doing that, haven't told enough you're doing/how you're using give meaningful advice fit needs best.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -