I want to use std :: shared_ptr in a read / write script. One thread constantly receives new information and stores a smart pointer to the latest data. When the time comes to start my slow calculation, I take a smart pointer to all the data to make sure I'm looking for consistent data. In the example below, when I use a and then b, I know that they belong to each other.
I'm not sure if atomic_source and atomic_string should be used here? I don’t care which version of Foo I am considering as long as it is consistent and valid.
So, should atoms be used here on their smart pointers so that this code works from two different threads?
Thank,
Floor
#include <iostream>
#include <memory>
class Foo{
public:
int a;
int b;
};
class MyClass{
public:
std::shared_ptr <Foo> lastValue;
void realTimeUpdate (Foo* latest) {
lastValue=std::shared_ptr <Foo> (latest);
};
void doSlowCalcFromAnotherThread () {
std::shared_ptr <Foo> stableValue=lastValue;
std::cout<<"a: "<<stableValue->a<<std::endl;
std::cout<<"b: "<<stableValue->b<<std::endl;
};
};