私はここでこの古いBoost Thread FAQを読んでいます。boost::mutex
コピー不可オブジェクトをメンバーとするクラスのコピー作成と代入演算子を実装するガイドラインがあります。コピーできないクラスの代入演算子boost :: mutex
私はコピーコンストラクタでうまくいますが、私は代入演算子に疑念があります。 以下の説明はまだ有効ですか?
// old boost thread
const counter & operator=(const counter& other){
if (this == &other)
return *this;
boost::mutex::scoped_lock lock1(&m_mutex < &other.m_mutex ?
m_mutex : other.m_mutex);
boost::mutex::scoped_lock lock2(&m_mutex > &other.m_mutex ?
m_mutex : other.m_mutex);
m_value = other.m_value;
return *this;
}
が、これはにアップデートしてはいけません。
// new boost thread
const counter& operator=(const counter& other){
if (this == &other)
return *this;
boost::unique_lock<boost::mutex> l1(m_mutex, boost::defer_lock);
boost::unique_lock<boost::mutex> l2(other.m_mutex, boost::defer_lock);
boost::lock(l1,l2);
m_value = other.m_value;
return *this;
}
私はちょうど、技術的には、FAQで与えられた解決策が必ずしも機能しないことを指摘したいと思います。そのようなポインタを比較することは明記されておらず、*両方の*比較に対して 'true'または' false'を返す可能性があります。適切な方法は、 'void *'や 'boost :: mutex *'に 'std :: less'(とkin)の特殊化を使うことです。なぜなら、関数比較のファンクタは全てのポインタの特殊化に対して全順序付けを行うからです。 (まだ*特に*の結果は保証されていませんが、有効な注文が保証されています) – GManNickG