2015-12-08 04:51:55 +08:00
|
|
|
#pragma once
|
2015-09-13 17:34:17 +08:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
|
2017-07-06 23:47:28 +08:00
|
|
|
// I heard this is patented.
|
2017-02-18 18:54:37 +08:00
|
|
|
template <class T>
|
|
|
|
class atomic_shared_ptr final {
|
|
|
|
public:
|
|
|
|
atomic_shared_ptr(std::shared_ptr<T>&& ptr) : ptr(ptr) {}
|
2015-09-13 17:34:17 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
std::shared_ptr<T> load() { return std::move(std::atomic_load(&ptr)); }
|
2015-09-13 17:34:17 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
bool compare_exchange_weak(std::shared_ptr<T>& expected,
|
|
|
|
std::shared_ptr<T> desired) {
|
|
|
|
return atomic_compare_exchange_weak(&ptr, &expected, desired);
|
|
|
|
}
|
2015-09-13 17:34:17 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
private:
|
|
|
|
std::shared_ptr<T> ptr;
|
2015-09-13 17:34:17 +08:00
|
|
|
};
|