は、異なるユーザが同じリソースへの同時要求をすることができ、実際のシナリオをモデル化し、以下の単純化されたプログラムを考えてみましょう:同期破壊のstd :: shared_ptrの通じ<T> ::リセット()
#include <thread>
#include <memory>
#include <mutex>
#include <iostream>
using namespace std;
struct T {
void op() { /* some stuff */ }
~T() noexcept { /* some stuff */ }
};
std::shared_ptr<T> t;
std::mutex mtx;
std::weak_ptr<T> w{t};
enum action { destroy, op};
void request(action a) {
if (a == action::destroy) {
lock_guard<mutex> lk{mtx};
t.reset();
std::cout << "*t certainly destroyed\n";
} else if (a == action::op) {
lock_guard<mutex> lk{mtx};
if (auto l = w.lock()) {
l->op();
}
}
}
int main() {
// At some point in time and different points in the program,
// two different users make two different concurrent requests
std::thread th1{request, destroy}; std::thread th2{request, op};
// ....
th2.join();
th1.join();
}
場合、私は求めていないのですプログラムは正式に正しいと思いますが、私はそうだと思いますが、スマートポインタ経由で共有されるの同期破壊の同期破壊を保証するこのアプローチは見たことがありません。私は個人的にそれが罰金であり、有効な使用を持っていると思います。
unique_lock
との古典的な同期や条件変数の変更、さらにはT
への変更(例:アトミックフラグ)の導入を除いて、より洗練された選択肢があるかどうか、他の人が同じと思うかどうかは疑問です。
何とか私がmtx
を取り除くことができれば理想的でしょう。
ルーツが見えます。通常の警告では、同時に呼び出されたときに 'T :: op'が正しい必要があることが適用されます。 –
しかし、shared_ptrは決して何にも初期化されていませんか?そして、あなたはファイルレベルの範囲でそれをする必要がありますか? – AndyG
すべての変数は、実際のプログラムで初期化されます。 – Martin