Skip to content

Instantly share code, notes, and snippets.

@overminder
Last active August 29, 2015 14:06
Show Gist options
  • Save overminder/55a0a88b3823178441b1 to your computer and use it in GitHub Desktop.
Save overminder/55a0a88b3823178441b1 to your computer and use it in GitHub Desktop.
http://www.zhihu.com/question/25037476/answer/30100671
class mutex {
public:
void acquire();
void release();
};
template <class T>
class AtomicValue {
T *p;
mutex m;
public:
AtomicValue(const T &t)
: p(new T(t))
{ }
~AtomicValue() {
m.acquire();
if (p) {
delete p;
p = 0;
}
m.release();
}
};
void test() {
AtomicValue<int> ai(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment