
Steven Watanabe wrote:
As mentioned, that solution causes can cause an unresolvable race condition. For example this class could not be used in a variant then.
class C { public: explicit C(int i = 0) : n(i) { register_object(this); } ~C() { unregister_object(this); } static void increment_all() { std::for_each(objects.begin(), objects.end(), ++*_1); } C& operator++() { boost::mutex::scoped_lock l(m2); ++n; return(*this); } private: static std::set<C*> objects; static boost::mutex m1; void register_object(C* c) { boost::mutex::scoped_lock l(m1); objects.insert(c); } void unregister_object(C* c) { boost::mutex::scoped_lock l(m1); objects.erase(c); } boost::mutex m2; int n; };
I see the problem now. If you move the object, then the pointer in the set doesn't point to a valid object anymore. Would a move constructor that replaces the pointer in the set completely solve the problem?