Question

I want to store a boost::posix_time::ptime object in a Windows shared memory map for multiple processes.

However, for this to be safe, ptime cannot have pointers to other places in memory (i.e. it needs to be a POD) or else the various processes accessing the memory map will run into problems because parts of the ptime object are owned by another process.

So is ptime safe to store in a Windows shared memory map?

Was it helpful?

Solution

I ended up converting boost's ptime into microseconds based off the Unix epoch, like this:

#define MICROSECONDS_PER_SECOND 1000000
boost::posix_time::ptime unixEpoch(boost::gregorian::date(1970, boost::gregorian::Jan, 1));

boost::posix_time::time_duration deltaTime = timestamp - unixEpoch; 
boost::int64_t microsecondsAfterEpoch =
    deltaTime.ticks() / (deltaTime.ticks_per_second() / MICROSECONDS_PER_SECOND);

I could have easily just used deltaTime.ticks(), but then I would also have needed to keep track of deltaTime.ticks_per_second(). For my case, microseconds was enough precision.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top