Take the 2-minute tour ×

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?

share|improve this question
Whatever the current implementation, nothing is guaranteed in the documentation so nothing is guaranteed to be the same in upcoming versions of Boost... –  ildjarn Jan 4 '12 at 21:13
Consider copying what you need out of ptime into a your own POD structure and then shove that in shared memory. –  jeffamaphone Jan 4 '12 at 22:25
add comment

1 Answer

up vote 1 down vote accepted

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

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.

share|improve this answer
add comment

Your Answer


