Wednesday, 10 August 2011

workaround for C++ clock_gettime gettimeofday tv_usec tv_nsec make rand() srand() MAC OSX Lion / Intel fix

Instead of using gettimeofday or clock_gettime use below:-

#include <mach/mach_time.h>
#include <mach/mach.h>
#include <mach/clock.h>

// OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;

// make unique
srand( (unsigned)time( NULL ) + ts.tv_nsec);
long random = rand()+ ts.tv_nsec

No comments:

Post a Comment