#include "../Mutex.h" #include "../Thread.h" #include #include #include using namespace muduo; class Foo { public: void doit() const; }; typedef std::vector FooList; typedef boost::shared_ptr FooListPtr; FooListPtr g_foos; MutexLock mutex; void post(const Foo& f) { printf("post\n"); MutexLockGuard lock(mutex); if (!g_foos.unique()) { g_foos.reset(new FooList(*g_foos)); printf("copy the whole list\n"); } assert(g_foos.unique()); g_foos->push_back(f); } void traverse() { FooListPtr foos; { MutexLockGuard lock(mutex); foos = g_foos; assert(!g_foos.unique()); } // assert(!foos.unique()); this may not hold for (std::vector::const_iterator it = foos->begin(); it != foos->end(); ++it) { it->doit(); } } void Foo::doit() const { Foo f; post(f); } int main() { g_foos.reset(new FooList); Foo f; post(f); traverse(); }