Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] How to serialize an atomic<> value?
From: Robert Ramey (ramey_at_[hidden])
Date: 2015-12-20 14:58:46


On 12/20/15 10:05 AM, Merrill Cornish wrote:
> I am running Boost_1_60_0 on Windows 10 64bit on MinGW64:
>
> I have this class data member I want to serialize:
>
> std::atomic< std::time_t > mUpdate;
>
> Since I'm on a 64-bit machine, serialization recognizes that as atomic<
> long long int >. However, given
>
> ar & mUpdate;
>
> serialization says it does not know how to serialize atomic< long long
> int >. My solution, which compiles cleanly, is
>
> std::time_t updateTime = mUpdateTime.load();
> ar & updateTime;
> mUpdateTime = updateTime; // assign is atomic
>
> My reasoning is that on load (ar => mUpdateTime), the first line is
> useless, but harmless. Similarly, on save (ar <= mUpdateTime), the last
> line is redundant, but also harmless. Of course, the application is not
> executing during serialization and de-serialization.
>
> As I said, this compiles without error or warning; but I worry that I
> have missed something.
>
> Any thoughts?

You have missed something. You can easily make small serialization
implementations for any serializable type. You do it just as you would
for you own type. look in the manual under non-intrusive serialization.
  Look for examples in the library.

given that it looks like std::time_t already has serialzation support,
you'll have to do it for atomic<T>. This will look something like:

template<typename Archive, typename T>
void serialize(Archive & ar, std::atomic<T> t, unsigned version){
     ar & t;
}

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net