atomic operations for shared_ptr ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello


what are the current plans for supporting atomic_* free functions for shared_ptr, as defined in C++11 (20.6.2 "Header <memory> synopsis")? These would be very useful for passing shared_ptr objects between threads, esp. if there are multiple readers and single writer.

Currently the only safe and viable way to prevent race condition (when smart pointer being copied in one thread is concurrently reset in another) is to always use mutexes when accessing these objects, but that is more expensive than it needs to be, esp. if multiple readers are trying to read the data at roughly the same moment (which is typical in publisher/subscriber model).

Example:

  shared_ptr<Data> data; // Written by one thread, read by many
  mutex            mutex; // Required due to lack of atomic_ overloads

  thread 1:
  auto newData = make_shared<Data>(a, b, c);
  {
    lock_guard l(mutex);
    swap(data, newData);
  }

  thread 2:
  shared_ptr<const Data> readData;
  {
    lock_guard l(mutex);
    readData = data;
  }
  // act on readData

  thread 3: (same as thread 2)
  thread 4: (same as thread 2)
  etc.

... could be replaced with, hopefully more efficient:

  thread 1:
  auto newData = make_shared<Data>(a, b, c);
  atomic_store(&data, newData);

  thread 2:
  shared_ptr<const Data> readData = atomic_load(&data);

  thread 3: (same as thread 2)
  thread 4: (same as thread 2)
  etc.


Current behaviour, g++ 4.8.1 (mingw):

#include <memory>
#include <atomic>

int main()
{
  std::shared_ptr<int> a;
  std::atomic_store(&a, std::shared_ptr<int>());
}


In file included from main.cpp:2:0:
d:\programdata\mingw\include\c++\4.8.1\atomic:890:5: note: template<class _ITp> void std::atomic_store(std::atomic<_ITp>*, _ITp)
     atomic_store(atomic<_ITp>* __a, _ITp __i) noexcept
     ^
d:\programdata\mingw\include\c++\4.8.1\atomic:890:5: note: template argument deduction/substitution failed: main.cpp:7:47: note: 'std::shared_ptr<int>' is not derived from 'std::atomic<_ITp>'
   std::atomic_store(&a, std::shared_ptr<int>());
                                               ^
In file included from main.cpp:2:0:
d:\programdata\mingw\include\c++\4.8.1\atomic:895:5: note: template<class _ITp> void std::atomic_store(volatile std::atomic<_ITp>*,
_ITp)
     atomic_store(volatile atomic<_ITp>* __a, _ITp __i) noexcept
     ^
d:\programdata\mingw\include\c++\4.8.1\atomic:895:5: note: template argument deduction/substitution failed: main.cpp:7:47: note: 'std::shared_ptr<int>' is not derived from 'volatile std::atomic<_ITp>'
   std::atomic_store(&a, std::shared_ptr<int>());
                                               ^


B.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux