リーダー/ライターのシナリオでstd :: shared_ptrを使用したいと考えています。あるスレッドは常に新しい情報を受け取り、最新のデータへのスマートなポインタを保持します。私の遅い計算を実行する時が来たら、私はすべてのデータにスマートなポインタをとり、一貫性のあるデータを確認しています。下の例では、aとbを使用すると、それらが一緒に属していることが分かります。1つのリーダーと1つのライタースレッドを持つときにstd :: atomic_を使用する必要がありますか?
ここでatomic_loadとatomic_storeを使用する必要があるかどうかわかりません。一貫性があり有効である限り、私が見ているFooのバージョンはあまり気にしません。
このコードを2つの異なるスレッドから処理するには、ここで私のスマートポインタにアトミックを使用する必要がありますか?
おかげで、
ポール
#include <iostream>
#include <memory>
class Foo{
public:
int a;
int b;
};
class MyClass{
public:
std::shared_ptr <Foo> lastValue;
void realTimeUpdate (Foo* latest) { //takes ownership of Foo
lastValue=std::shared_ptr <Foo> (latest); //Is this OK to do without using std::atomic_?
};
void doSlowCalcFromAnotherThread() {
//take a reference to all input data
std::shared_ptr <Foo> stableValue=lastValue; //Is this OK to do without using std::atomic_
//display a and b guaranteed that they come from the same message
std::cout<<"a: "<<stableValue->a<<std::endl;
std::cout<<"b: "<<stableValue->b<<std::endl;
};
};
おそらく 'std :: mutex'を使ってポインタへのアクセスを保護し、それを1日と呼びます。 'std :: shared_ptr'への代入はアトミックではありません。 –