これは、データ競合を報告するthreadsanitazer (clang) からのペーストです http://pastebin.com/93Gw7uPi
グーグルで調べてみると、これはthreadsanitazerの問題のようです(たとえばhttp://gcc.gnu.org/bugzilla/show_bug.cgi?id=57507)
だから、これが次のように見えるとしましょう(これは今手作業で書かれているので、コードが機能していません):
class myclass : : public std::enable_shared_from_this<myclass>
{
public: // example!
myclass(boost::asio::io_service &io, int id);
~myclass() { /*im called on destruction properly*/ }
void start_and_do_async();
void stop();
int ID;
boost::asio::udp::socket usocket_;
... endpoint_;
... &io_;
}
typedef std::shared_ptr<myclass> myclass_ptr;
std::unordered_map<int, myclass_ptr> mymap;
myclass::myclass(boost::asio::io_service io, int id) : io_(io)
{
ID = id;
}
void myclass::start_and_do_async()
{
// do work here
//passing (by value) shared_ptr from this down in lambda prolongs this instance life
auto self(shared_from_this());
usocket_.async_receive_from(boost::asio::buffer(...),endpoint,
[this,self](const boost::system::error_code &ec, std::size_t bytes_transferred)
{
start_and_do_async();
}
}
void myclass::stop()
{
// ...some work and cleanups
usocket_.close();
}
メインスレッドで新しいスレッドが作成され(これは実際には別のクラスにあります)、新しい io_service ハンドラに対して実行されます
new boost::thread([&]()
{
boost::asio::io_service::work work(thread_service);
thread_service.run();
});
メインスレッド要素から定期的に追加または削除されます
void add_elem(int id)
{
auto my = std::make_shared<my>(thread_service, id);
my->start();
mymap[id] = my;
}
void del_elem(int id)
{
auto my = mymaps.at(id);
mymap.erase(id); //erase first shared_ptr instace from map
// run this in the same thread as start_and_do_async is running so no data race can happen (io_service is thread safe in this case)
thread_service.post[my]()
{
my.stop(); //this will finally destroy myclass and free memory when shared_ptr is out of scope
});
}
したがって、この場合、ドキュメント (個別の shared_ptr (boost または std) が複数のスレッドからの読み取り/書き込みアクセスを許可すると記載されている) から判断すると、データ競合が発生する可能性がありますか?
このコードは、1 つのポインターに対して 2 つの別個の shared_ptr インスタンスを適切に作成しますか?
shared_ptr.h でアトミック操作を確認できるので、誤検知を報告するスレッド サニタザーに問題があることを確認したいだけです。
私のテストでは、これはメモリ リーク (shared_ptr インスタンスが適切に削除され、デストラクタが呼び出される)、セグメンテーション違反など (要素の挿入/削除に 10 時間 - 1 秒あたり 100 または 1 秒ごと) なしで正しく動作します。