C ++ shared_ptr和threadsanitazer报告数据竞争

时间:2014-01-04 13:03:30

标签: c++ multithreading boost-asio shared-ptr thread-sanitizer

这是来自threadsanitazer(clang)的一个粘贴报告数据竞争 http://pastebin.com/93Gw7uPi

在Google上搜索这似乎是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)允许来自多个线程的读/写访问)判断是否存在数据竞争?

此代码是否为一个指针正确创建了两个不同的shared_ptr实例?

在shared_ptr.h中我可以看到原子操作,所以我只想确认线程清理报告误报是有问题的。

在我的测试中,这可以正常工作,没有内存泄漏(正确删除shared_ptr实例并调用析构函数),段错误或其他任何内容(插入/删除元素10小时 - 每秒100次或1秒)

1 个答案:

答案 0 :(得分:1)

假设shared_ptr线程安全文档与其实现相匹配,那么shared_ptr上的数据竞争报告是误报。线程在shared_ptr的不同实例上运行,这些实例共享同一实例的所有权。因此,不存在同一shared_ptr实例的并发访问。

话虽如此,我想强调的是,在示例中,myclass::usocket_的线程安全性仅依赖于处理io_service的单个线程,有效地在隐式{{3}中执行}。如果多个线程为io_service提供服务,则可以使用显式strand来提供线程安全性。有关Boost.Asio和strands的一些线程安全细微差别的更多详细信息,请考虑阅读strand答案。